When we code a game in C#, whether with Unity or any other tool, we need to ‘know’ what the situation in the game is. As an example, things like how many lives the player has, how many people connected to game-lobbies, where in the game level all the players and enemies are, etc. The game’s variables are the part of our code that allows us to do this. Variables are our game’s data.
[widgets_on_pages id=”udemy_advert_unity_1″][widgets_on_pages id=”udemy_code_details”]
All of these variables are part of the data of the game. And it is very easy to think of lots more examples of data, a typical game might need.
Consider we might need data to describe the game environment, artificial intelligence or the layout of the different levels of a game? All this information, even in a simple platform game for example, will become fairly complicated once the whole game is built.
C# begins to solve this problem with variables. Variables enable us to easily store data in a memorable/meaningful and flexible way. We mentioned a moment ago that we would need to remember the player’s lives. To manage this particular piece of data we can create a variable in our code and we can make it memorable by giving this variable an appropriate name. Let’s call our variable to hold the player’s number of lives, playerLives.
Variable naming conventions
You might have noticed that the name is simply a truncation of the two words, with the first letter of the first word in lower case and the second letter of the second word in upper case. In C# we can call our variables almost anything we want but by using meaningful names there is never any doubt about what the variable is for.
Furthermore, by using the combination of lower and upper case letters we are adopting a naming convention. By sticking to a naming convention, especially when our code gets more complicated, it will make it more easily readable and less likely for mistakes to occur.
Here we are using the simple lower case word followed by upper case word because it makes the second word clear and when we introduce more types of C# code later in the course it will make our variables stand out from other types of code.
C# variable types
Before we can begin to write code with variables we must choose what type of variable each will be. Types are simple to understand. It is just a case of thinking about what information (what type) any particular variable will be used to store. In the case of our playerLives variable we are storing a number. But let’s consider some other potential variable types.
What about the name of the player, perhaps we could use a variable called playerName. Here we don’t want to store a number but a collection of letters from the alphabet that form the players name. Think perhaps also about data that stores if something has occurred- or not, that is true or false. This is much more common than it might first seem and some examples soon will help. Also consider we might need to use different types of number, just like in the real world. Whole numbers, fractions etc.
To avoid any ambiguity about what type a variable is, C# makes us declare a type before we can use a variable. This makes it plain what that variable will be used to store. The C# compiler then knows how to deal with our variable.
[widgets_on_pages id=”udemy_advert_games_category”][widgets_on_pages id=”udemy_code_details”]
Let’s have a look at some common types in C#. The list below is only a few of the available types in C#; all the variable types should appear fairly straight forward with the exception of one really powerful variable type, known simply as an object or class, which will be covered in a later tutorial. Note also that the different ways in which we can handle these variables will also be discussed in a more advanced variables tutorial later. But we can do a lot without worrying about that yet.
Type | Explanation |
---|---|
int | For whole numbers, negative as well as positive. |
float | For storing numbers with a floating point. Numbers with a decimal fraction (1.5 or 3.141 etc). |
bool | This is a special variable that can only be one of two values; true or false. This type of variable is perfect for making decisions about what to do next. Is the enemy shooting? Is the game over? And much more. It is named after the eminent mathematician George Bool. |
Class object | Class objects can be nearly anything at all. They are a special type of variable which not only stores values but also behaviour. The player, a game character or even the game itself could be an object. |
The table shows some key variable types that will help us to make some simple games in C#. The last one, Object, is the most powerful of all and will be covered in a later tutorial. We will however start meeting some objects/classes straight away in the Unity projects.
How variables work
We can think of a variable as a kind of storage space. And when we declare a variable along with its type C# not only reserves a space in memory, appropriate for that type but also links the name we give it to that space. So whenever we refer to playerLives we access whatever value is stored at a location in memory set aside for it. We don’t need to worry about how it works just yet or where in our computer’s RAM/CPU it is kept. In a later tutorial we will look into this more however. This leaves us free to work on the important aspects of our game without concerning ourselves about how computer memory is managed by the device our game is being played on.
We prepare our variables in two steps; declaration followed by initialization. Let’s have a look at what the C# code for this looks like.
Declaring a variable
We can declare a variable in C# like this:
int playerScore; float valuePi; bool isAlive;
Above we simply state the type followed by the variable name and end the line with a semicolon. We add the semicolon to the end of the line to tell the C# compiler that it is the end of the line. Notice that there are no object examples as they will be dealt with in later tutorials.
On line 1 of our code we have an int type variable (for numbers) called playerScore and on line 2 a float type variable called valuePi which can hold a decimal fraction that could represent pi (3.141…). Then on line 3 we declare a variable of type bool to hold a true or false value, perhaps as to whether or not the player is still alive.
Initializing a C# variable
Now we have declared the variables with meaningful names we can initialize those same variables with appropriate values, like this:
playerScore = 0; valuePi = 3.141f; isAlive = true;
Notice on the end of the float initialization we append an f. This is required in C# to distinguish the number from other types that we can use. We could optionally combine the declaration and initialization into one step, like this:
Declaring and initializing in one step
int playerScore = 0; float valuePi = 3.141f; bool isAlive = true;
What next?
Of course, to make our variables any use at all we need to be able to read them and manipulate them. So let’s learn about changing the value of game variables with expressions.
Leave A Comment