Variables are defined names that stores a specific type of value. The logic behind the health of a character in a video game has a variable that holds the value of the health. We as programmers have to define what meaning a variable will hold. But first, let's go back to Algebra.
In algebra, for instance, you can be given the definition:
This defines the variable x to be equal to the value 8. Do you see what is going on here? The equal sign assigns the value 8 to x.
Now you know that z = 12 because the variable x = 8 and 8 + 4 = 12.
Let's try a complicated one.
The variable z equals the value of 7. The values assigned to x, y, and z are 5, 12, and 7.
Now that we have an understanding of how variables are assigned, it works similarly in a programming language.
Variables in C#
There are value and reference types of variables to understand. It is very important to know how data types are handled within a program. I won’t go into detail since it’s still a topic I am learning and exploring.
Value Types: when created, a single space in memory is allocated to store the data and that variable directly holds a value. It contains an instance of the type. Primitive data types such as int, bool, float, enums, structs, and char are of a type value.
Each variable has its own copy of the data. It is not possible for operations on one variable to affect the other.
Reference Types: when created, it is used by a reference that holds a reference (memory address) to the object but not the object itself. It contains a reference to an instance of the type. Strings, classes, interfaces, and delegates are of type reference.
Two variables can reference the same object. Therefore, operations on one variable can affect the object referenced by the other variable.
If that confused the hell out of you, don’t feel ashamed. It’s hard to grasp at first but the more you research and practice, the more you will understand it!
Let’s go a bit deeper. Here’s an example of a variable initialized and declared:
Now that we know how variables are initialized and declared, let's take a closer look at what makes a variable.
Access modifier + Data type + Identifier = value;
Access modifiers control the accessibility of a variable. There are 3 common ones:
- Public: public variables can be accessed outside of the class.
- Private: private variables can only be accessed within the class.
- Protected: protected variables can only be accessed within inherited classes.
Different data types have different sizes that are stored in a variable. It defines what can be stored in the variable. Here are some common data types that you’ll encounter frequently. Int, float, string, and bool.
To check out more, head over to the official documentation of the C# programming language.
- int- an integer data type. Can store Whole numbers such as 1, 5, 101, 523.
2. float- a float is a floating-point data type. It stores Real numbers such as 1.3f, 5.6f, 55.8f, 104.f, etc. Notice the ‘f’ after the value? When declaring a float value, it requires an ‘f’ at the end before the colon.
3. string- a string is a string of characters that can take a single character to an entire book within quotation marks. Some examples: “You”, “I am someone”, “1 2 and 3”, “< an entire book here, or perhaps all kinds of symbols@! > “.
4. bool- a boolean is a conditional data type. It returns “true” or “false.”
In Unity3D, there are other data types specifically made for the engine. Here are a few examples: Vector3, Vector2, Color, and Quaternions to name a few.
Identifiers are what you as a programmer name your variables. You identify a variable's name for what it will store. If I want to identify the health of a player, I would include “health” in the variable name. For instance, a health system in a Zelda game has a heart variable of type float for the full and half hearts it displays on the screen. I would imagine their “heart” variable initialized and declared would be like:
An array of floats to store all the hearts in a variable. This is the stuff a programmer must think and solve. How will the health system work? What data type should I use? What should I name the variable? What will the scope be?
There are common conventions when creating variable names. If it’s private, start your variable name with an underscore. An example: “_privateVar.” If a variable is public, start it with a lowercase letter. An example: “publicVar.” Depending on the company, they might have certain naming conventions every employee will have to abide by.
Keep in mind, variable names cannot begin with a number. You will get a compiler error.
Also, variable names should be camelCase. Here are a few examples:
There are exceptions, a static variable name should be all in caps. Ex:
Another bit of information is that you don’t have to include the access modifier when initiating a variable. If it’s not there, it will automatically be private. A good practice is to explicitly type in the access modifier.
I read a part in a book called “Code Complete” from Steve McConnell and I love the description he gave about the scope. The scope is a way of thinking about a variables celebrity status. How famous is it?
Global scope means that any code can see and reference the variable.
Local scope means its visibility is limited to a block of code. Usually within a method. It exists when a method is called, and it automatically gets discarded when the method ends.
To note, it is good practice to initialize and declare all variables you create from the start. It’s an inexpensive form of defensive programming. You will have fewer bugs down the road.
In Unity3D, use the SerializeField attribute above or beside your private variable if you have game designers tweaking the settings of the game. This way, your variable’s visibility can only be seen through the inspector panel and modified from there.
Not quite a depth guide. A bit messy here and there but I hope anyone who reads it can learn something from this. There is a lot to learn, and as I go through my journey of becoming a better programmer, I can come back to this topic and write a much more detailed version.
Thank you for reading!