Variables, the building blocks of programming
I had planned to write about Unity’s new input system and how we could recreate yesterday’s player movement exercise using the newer system, but I’m also in the middle of moving to a new state this week and so we’re Going to look at something a little less time intensive to write about! Fear not! The article about the input system will still be coming but it will most likely be postponed until next week after I have finished the journey to my new home. With that said, let’s dive into variables!
In programming there is often a need to store a value for use elsewhere in the code. In these cases there is a mechanism, called a variable, that acts as a container for the data that needs to be stored and later used. We can use the example of a box to model what variables do. Say we have a box and we have a ball, we can place the ball, which would represent the data, into the box, representing the variable, and write ball on the box so that we know what is in it when we want to find and use the ball later.
In C#, variables have four parts, with three parts being required and the fourth being optional. A name, an accessibility level, a type, and optionally a value. The name is a moniker the programmer gives the variable so that they can reference it from other parts of the code. In our example above, ball, written on our box, would have been the name component.
The accessibility level describes what parts of our code can see and access our variable. Some examples are public, private, and protected. Public variables are accessible by code outside of the class they are defined in, private variables are accessible only by the class they are defined in, and protected variables are accessible within the class they are defined in as well as any classes that are related to that class such as derived child classes. In our example of the box is there any one can see it and it is open so that anyone can see inside of it, that would be an example of it having a public accessibility, if it were closed and placed in a closet that only we could go into, that would be like having a private accessibility.
The type component of our variable defines what kind of data can be stored in the variable. Data types can include number types like integers, and floating point numbers, true/false values called boolean values, or more complex types like strings characters, or custom objects that can be a combination of other types. In our example the ball itself is of type ball, so our box, has been defined as holding a ball type.
Lastly is the optional value. The ball itself would be our value. The box could have been designated to hold objects that are of type ball but not had a ball placed in it yet, or it could have a red ball, or a baseball, or a football. These are all ball objects that have different properties but they are all balls and so in this example could all be placed in the box.
In my previous article about player movement we stored a speed variable so that we could define and retrieve a numeric value that was then available to the player class for use in the movement calculation.
Next time
We have taken a look at what a variable is, what we can use them for, how they are structured, and some examples of how they might relate to real world counter parts. Tomorrow, we will explore the concept of writing pseudo code and why we should be doing it as programmers. If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.