Hey there, Gamers and Game Makers!
In the this week's blog, we are going to take a look at the first in a series of C# tutorials that are aimed at absolute beginners. The first tutorial will look at variables are and how to use them. So, let's dive right in!
We'll be using unity for this series of tutorials. You can grab the latest version from the Unity site here.
Ok, so first off, what exactly is a variable? A variable is essentially something that stores information. A variable has to have a name. This can be anything you want but it is best practice to your variables meaningful names. For example, it would be confusing if you had your variable for health named something like damage. This would cause confusion later when you need to tweak your code. A better name would simply be health.
While we can call a variable anything we want, we do have to specify what type the variable is. In C#, we have variable types such as:
A variable of type int can hold an integer value. This can be a positive or negative value. For example, we could declare an int variable called health for our player as their health can be best represented as an integer value.
A variable of type float can hold a decimal number such as 6.84. These can also be positive or negative values. We can use floats to store more accurate readings for things such as speed, force and other physics related methods in Unity.
A variable of type Bool is used to store a true or false value.. We can use Bools to trigger events in our scenes. For example if you had an area of lava in the game that would kill the player if they were to come in contact with it, you could have a Bool called playerHitLava set to false by default. It could check a collision trigger (don't worry, we'll cover triggers in later tutorials) on the lava that once the player comes in contacts with updates the Bool variable to true. Once that variable is set to true, it could run a sequence of events that would set the players health variable to zero and fade out the camera.
A variable of type String is used to contain text information. You can put whatever you want in a String but you must use quotation marks. For example, "This is the contents of a String". We use Strings all the time to relay information to a player.
Ok, let's try making some variables. With your Unity editor open, the first thing you'll want to do is create a new C# script. To do so, right click in the assets window, go to create and then click C# script. This will make a new blank C# script in your current asset folder. You can name this whatever you want. For the purpose of this tutorial, I'm going to call mine, VariableTest. Next, double click on it to open the script in Mono Develop. This is the code editor used by Unity.
You'll be presented with a window like this. this is the current contents of your script. The code you already see is what unity creates when making the script as it's the required format for C# scripts in the engine. For now, let's not worry too much about the stuff at the top. These are import utilities used by Unity and I'll cover them in a future post. Right now, we're going to look at declaring our variables.
Ok, so I've created a variable for our players health simply called playerHealth. I did this by first declaring the variables type which in this case is an int. I then gave the variable a name, playerHealth Notice how the name begins with a lower case character but the second word begins with an uppercase character? This is called camel casing and is standard practice when writing variable names in programming. I now need to assign an initial value to the characters health.
I do this by simply adding an = sign followed by the value I want to assign. In this case, I'm giving the player an initial health value of 100. Make sure you always add a semicolon at the end. That's it. you've now declared your variable type, gave it a name and assigned a value to it. So, as you can see, declaring variables follows a simple pattern of type, name and value.
example: int playerHealth = 100;
Ok, let's declare some more variables and display them.
Right, so now I've also declared a String variable called playerName and it's value is "Jack". So how would we go about displaying the information from these variables? First off, I now need to explain the next two sections of our scripts code sequence. The first line saying viod start, this is a start function and gets called once at the beginning of our game. I'll go into more detail on functions in a future tutorial.
The section, void Update gets called once every frame. So say for example your game is running at 60 frames per second, then this code is checked 60 times a second.
So, if we want to display information to the unity console, we can use the print function which is written simply print(); Whatever we put in the brackets is what will be displayed to the console window. For example I could put a string inside the brackets saying "Hello" and it will be printed to the console. Let's try displaying the word Hello in the console. Type print followed by brackets within the curly brackets of the void start function. Once you have done this save the script and go back into the unity editor. In order for our script to work, we first need to assign it to an object. Drag the script onto the main camera. Now run the scene.
As you can see, the script is now active at runtime and is printing our Hello string to the console window.
We can also print our variables to the console or to be more exact, their values. For example, if I type the health variable name in the brackets of the print function, it will print the value of the variable to the console.
As you can see the value of the variable playerHealth has been printed to the console window.
Ok, now let's try displaying multiple variable values at once in a meaningful way. How would I get the console to say hello to the player and tell them their current health? We use a method called concatenation. This is where we join multiple things together to display them all at once in the console window. So, as before in the brackets of our print function type a string "Hello " now add a + followed by the playerName variable followed by another + then followed by a string " your health is " followed by another + finally followed by the playerHealth variable. It should look as follows:
print("Hello " + playerName + " your heath is " + playerHealth)
As you can see, the console now displays Hello Jack your health is 100. Concatenation is a great way of combining many variables to give us various results in our games. That about does it for this tutorial on variables. We'll dive a bit deeper into what we can do with our code in the next tutorial.
As always, if you have any questions,leave a comment below or feel free to get in touch.
Until next time!