‘Variables’ are essential in the process of creating software, and game development is no different. While it can seem obvious what variables are, there are more to them than one might think, and it’s even less obvious to understand how to work with them…

Variables are containers that can hold information. Traditionally in mathematics, we are used to using X as a variable and are often asked what value X has. When programming, X doesn’t need to be a number, it can be a text, a certain type of object, a binary value that can toggle between true and false, and any other data in our program. Variables provide a way to label data with descriptive names, so they can be clearly understood by us, and we’ll be able to keep track of, use and change what we’re working with.

Level Up | Episode 1 – Variables

You’ll often see variables being created with encapsulation, which is the level of accessibility they provide. “Public” variables can be accessed and seen from anywhere, provided you have access to the class, while “private” variables can only be accessed and seen inside the class. This is different from the scope of the variable. The scope dictates where the variable lives, and in which context it has meaning. A variable created in a “global” scope, lives in the entire game and will have a meaning everywhere as long as the game is running. This means that it’s not dependent on a certain object’s life or a certain class.

Meanwhile, a variable created in a “local” scope will be dependent on its scope’s survival – if it’s created inside of a class, it won’t have meaning outside of it. It does have meaning in nested scopes inside of the class though. For example, let’s say you’re in London. If someone asks where you are, you’d say you’re in London. Even if you’re in Liverpool Street, it’s correct to say you’re in London, because London is the scope of all places inside of it. However, it’s incorrect to say you’re in London when you’re in Brighton because that’s outside the scope of London.

There are many ways of working with variables. You can put restrictions on them, or expose them to the world of your game! You can make them static, and they will live as long as your game is running, like the global scope. But with static, you make sure you can only create your variable once. So you don’t want your bullets to be static, as you’ll be creating many of them, so be careful with making your variables static.

If you want to make sure that a variable you’re working with never changes its value, then make it a constant with the keyword “const”. Once you give it a value, it will be set for life and you’ll get an error every time you try to change it. This is a good practice to keep in mind for variables that refer to max/min values, like “maxHealth” and “minScoreToUnlockDoor” or any variable you don’t want to change throughout your game.

It’s important to know that C# is a case sensitive language, so in order for your variables to be recognised automatically by the program, you have to match the spelling of your variable exactly, including any capitalisation, when you use it, otherwise you’ll get an error.

And you want your variables to have several words to be descriptive, then you can’t use space. Instead, the naming convention is to use something called “camelCase”, which means you string the words together, starting with a small letter and from the second word you capitalise every first letter of each word.

Good luck with your coding! If you haven’t already, check out our Game Development Degree course at our Guildford and London campuses to turn your love of games into a career.


Digital Open Days

If you dream of achieving success in the creative industries, join us for our new Digital Open Day Experience. With Open Days over the coming weeks, we can’t wait to welcome you into the ACM community.

ACM Digital Open Day Experience

Related content