Reputation: 11
I'm a beginner to C# and I recently downloaded a Unity 2D platformer project off of Github. I'm trying to understand some of the coding inside of the project files for the player game object as I'm interested in learning how the physics in the game were created. The current bit of code that I'm trying to understand is shown below:
public float standingHeight = 40f;
public float ballHeight = 30f;
private float heightHalf
{
get
{
if (rolling || jumped) { return ballHeight / 2f; }
else { return standingHeight / 2f; }
}
}
I'm trying to understand what the variable heightHalf is trying to do. This variable appears to have a "get accessor" if I'm not mistaken. Am I right in thinking that the code is saying that if the player game object is jumping or rolling it will return the ballHeight variable divided by "2f"? Otherwise the value of standingHeight will be returned divided by "2f"?
Does this mean that the variable heightHalf will be assigned a value based on either of the two calculations mentioned in the previous paragraph? Can I ask why is a "Get Accessor" used instead of just an if statement? For example:
if (rolling || jumped) {
heightHalf = ballHeight / 2f;
}else{
heightHalf = standingHeight / 2f;
}
Upvotes: 1
Views: 166
Reputation: 117064
This variable appears to have a "get accessor" if I'm not mistaken.
This isn't entirely correct. heightHalf
isn't a variable, it's a read-only property. And a read-only property is defined by only having a "get accessor".
Am I right in thinking that the code is saying that if the player game object is jumping or rolling it will return the ballHeight variable divided by "2f"? Otherwise the value of standingHeight will be returned divided by "2f"?
Yes and yes.
Does this mean that the variable heightHalf will be assigned a value based on either of the two calculations mentioned in the previous paragraph?
Again, not a variable, but the property's values is as you described.
Can I ask why is a "Get Accessor" used instead of just an if statement?
Well, the get accessor using the if
statement. If we removed the get accessor you'd have to turn the property into a method. Here are the two legal C# ways of writing this:
(1) Read-Only Property
private float heightHalf
{
get
{
if (rolling || jumped)
{
return ballHeight / 2f;
}
else
{
return standingHeight / 2f;
}
}
}
(2) Method
private float heightHalf()
{
if (rolling || jumped)
{
return ballHeight / 2f;
}
else
{
return standingHeight / 2f;
}
}
The read-only property is the typical way to do this in C#. The method is the way Java would normally do this.
C# would normally use properties when the value is derived from the internal state of the object. A method would be used if there where input parameters that affected the output, i.e. a function.
Personally, if I were writing this code, I'd go with:
private float heightHalf => ((rolling || jumped) ? ballHeight : standingHeight) / 2f;
Upvotes: 0
Reputation: 132
Well done for understanding it. Yes, it is exactly doing as you described.
Personally I don't think there are preferred ways of doing it. If you're familiar with Java, they don't have get/set accessors. You could only do them either having a public property or making a get()/set(object value) methods.
C# gives you more control on how you can use property.
As I see it from the context of getting a heightHalf value, something needs this property in order to change the size of, well, my guess is a collider?
You could do it either from a get/set property, or a method called getHeightHalf().
However in lieu of the object oriented principles, you could say all methods of an object could be specified as an action. ie: a class that handles a player could have Jump(), Move(), Attack() functions.
Get/Set accessors provide the values that you need for those actions, hence the heightHalf logic is placed on a property instead of a method.
Upvotes: 2