1
\$\begingroup\$

I am coming back again into hobby game development after about 6 months away. I'm just trying to learn some kind of basic things again. I'm at the very start of making a very simple boxing management simulator. Later, I will be trying to re-learn using databases and might ask for more help.

But for now, I'm making a Boxer class which will hold all the data on each boxer (eg. Name, height, number of wins, punch power etc etc). I've used private variables for those and then have made my own methods for each variable called Getxyz().

I have seen people doing things like 'public int xyz { Get(); }' but i dont understand how that would work.

Is the way I'm doing ok (it does seem long-winded to me) or should I investigate these Getter Setters some more? Here's what I am doing right now:

public class Boxer : MonoBehaviour {
// generic boxer details:
int id;
string nameA, nameB;
int weight;
int height;
float age;
float reach;
// career info:
int wins, loses, draws, winsByKO;
// boxing attributes/stats:
float power, speed, agility, koPunch, stamina, toughness;

public int GetId()
{
    return id;
}
public string GetNameA()
{
    return nameA;
}
public string GetNameB()
{
    return nameB;
}

}

\$\endgroup\$
7
\$\begingroup\$

I have seen people doing things like 'public int xyz { Get(); }' but i dont understand how that would work.

These are called properties. They're a language construct that allows you to expose data from a type that looks like a field when you access it, but acts like a pair of functions in that you can have behavior execute whenever the property is read (the "getter") or assigned to (the "setter").

Consider this:

class Thing {
  private int secret; // This is a field.

  public int Secret { // This is a property.
    get {
      Debug.Print("Somebody is accessing the secret!");
      return secret;
    }

    set {
      Debug.Print("Somebody is writing to the secret!");
      secret = value; // Note the use of the implicit variable "value" here.
    }
  }
}

The Secret property has both a getter and a setter; these are really just functions that can execute any code you want. You can then do:

Thing thing = new Thing();
thing.Secret = 23; // Use the setter.
Debug.Print("Set the secret to {0}.", thing.Secret); // Use the getter.

Note that it looks like you're accessing a field here (no () is used to get or set Secret), but you're still actually calling the get and set functions you wrote above.

C# supports quite a bit of shorthand around properties. For example, declaring a property like you've seen:

public int Secret { get; set; }

Tells the compiler to generate a backing field for Secret for you, and set the getter/setter to some code that just returns or writes to that backing field.

Quite a bit more detail can be found at the MSDN link I shared at the start of this post.

Is the way I'm doing ok (it does seem long-winded to me) or should I investigate these Getter Setters some more?

What you are doing is fine, if you are happy with it. It will be worth your while to learn about C# properties, however. If nothing else, you will encounter them often in code other people have written even if you prefer not to use them yourself, so it will be good to understand them. It is also useful to know how to use properties because they are treated differently from regular fields in reflection, and many tools (Unity included) use reflection to find and identify data associated with your types. Some of those techniques will not work with simple fields because they assume the use of properties, and so you may need to use properties if you want to take advantage.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.