Search Unity

C# Code - Is this right?

Discussion in 'Scripting' started by jakeb_93, Jul 6, 2015.

  1. jakeb_93

    jakeb_93

    Joined:
    Jul 5, 2015
    Posts:
    11
    Okay, so i am new to C#, not new to programming as i know enough of HTML, CSS PHP etc to be able to code a few things, i also know a little of C++. To help me learn i have been taking online lessons and adapting them to fit my own future gaming needs. This is a script i have created that would display your max health, by multiplying your strength and endurance together.

    If you could spare a minute to look over the code, check it is correct And/Or suggest ways, methods etc that would help it improve or integrate better that would be fantastic :). Without Further Ado, my code:

    Code (CSharp):
    1. using System;
    2. namespace MaxHealthApplication
    3. {
    4.    class MaxHealth
    5.    {
    6.       // variables
    7.       double strength;
    8.       double stamina;
    9.       public void Acceptdetails()
    10.       {
    11.          strength = 4;  
    12.          stamina = 3;
    13.       }
    14.      
    15.       public double GetMaxHealth()
    16.       {
    17.          return strength * stamina;
    18.       }
    19.      
    20.       public void Display()
    21.       {
    22.          Console.WriteLine("Strength: {0}", strength);
    23.          Console.WriteLine("Stamina: {0}", stamina);
    24.          Console.WriteLine("MaxHealth: {0}", GetMaxHealth());
    25.       }
    26.    }
    27.  
    28.    class ExecuteMaxHealth
    29.    {
    30.       static void Main(string[] args)
    31.       {
    32.          MaxHealth r = new MaxHealth();
    33.          r.Acceptdetails();
    34.          r.Display();
    35.          Console.ReadLine();
    36.       }
    37.    }
    38. }
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,534
    How were you planning to apply this in practice and execute it? Also, why use doubles?
     
  3. jakeb_93

    jakeb_93

    Joined:
    Jul 5, 2015
    Posts:
    11
    Can i be super honest. I don't really know. I was following a tutorial, and it used this an example. Originally it was to work out the Area of a rectangle when given the width and the height. I looked at it at the end and went 'Hey! i can adapt this!'. I went through and changed any references of width, height and area, to strength, Stamina and Health.

    As for uses, i would imagine it being used for a character stats screen, showing Max Health. I hadn't really thought that far. I was just curious to see if i had actually learnt something. As for the doubles, i don't even know what they fully do.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    A double is just a floating point number. It's twice as big as a float, and therefore more precise. It's fairly standard in industry. But in Unity we normally use floats instead. Floats play nicer with current gen graphics cards, and are precise enough for most games. They only present issues when you start to get huge distances from the origin.
     
  5. jakeb_93

    jakeb_93

    Joined:
    Jul 5, 2015
    Posts:
    11
    OK so in the future just replace all 'Double's with floats.

    If I was doing a RPG game, this sorta script would be the sorta thing I would need to calculate the characters stats? Or have i gone off at a tangent correctly.