Hey and welcome!
Let’s start things off by breaking down what we’re seeing here starting with the data types.
When you’re creating a variable you’ll want to make sure that you define its data type so that the code will know what type of information is being stored in the variable.
These are the four most common data types available in C#:
- String (Used to store words and numbers to form sentences)
- Int (Stores whole numbers)
- Float (Used to store floating numbers/decimal numbers)
- Bool (Stores a true or false value)
So you should be able to spot in the image there that it’s showing an example of storing the relevant values after the equals sign there. Real quick tip here, when you’re dealing with variables the = symbol doesn’t mean what it traditionally means in maths and it’s better to think of it as a way of assigning a value to your variable.
playerName = “Garf”
So don’t think of the above as being “playerName equals Garf” but think of it more as “playerName shall be assigned the value of Garf” and you’ll end up with less heartache further down the road since there’s an operator we’ll be getting into that checks if one value equals another.
When to keep things private or public
Right let’s talk about these next, they’re used to determine whether your shiny new variable that you’re creating is going to be a public one or a private one. A private variable means that it’s only going to be available for use within your class/script you’ve declared it in and a public one means that separate scripts will be able to access them.
That’s all well and good but how is that relevant in relation to a game and how do I know when to use a private or public variable? Well let’s use the player script as an example which is assigned to our player object. Our player has certain stats attributed to them that only applies to the player such as their movement speed or how much inventory space they have, these are ideal to have as private since depending on your game an enemy object is going to be interested in a player’s speed or inventory slots which means the enemy script assigned to that enemy object won’t have a reason to access those variables in the player script.
In the case of an MMO you’ll be wanting the players to be able to see each other’s names so the player name variable for each player would be public.
It’s best to try and think ahead of time whether the variable you’re creating will need to be public or private but it’s good practice to stick with making your variables private until you find that they need to be public.
Telling at a glance if your variable is public or private
Speaking of good practice you may have noticed here that most of the variable names started with an underscore except for one. Well this is a .NET standard for keeping track of if your variable is private or public with the underscore showing that it’s private.
This is done so that if you’re working on a particularly large script with hundreds of lines of code you don’t need to scroll up to the top of your class where you’re declaring the variables to find if the one you’re looking at hundreds of lines down is private or not as you’ll be able to tell right away by the underscore so it’s good to keep that in mind as well when you’re creating your variables.
In the next one we’re going to be looking into If statements before we get back to the 2D game we’re making.