Adventures in GameDev with GameDevHQ! Day 9: Variables — The building blocks of programming!

Esteban Ibarra
3 min readMar 21, 2021

We talked about variables yesterday but let’s go into a deeper dive into them today. As stated before, variables are like boxes that contain your data. they hold your data until you take the items out of the boxes, maybe change them and put them back.

Why do we use variables? Besides the opportunity to make code easier to read, they are invaluable to making our game run. When you see a health bar, that isn’t really a health bar, that’s a variable named health! the amount of coins you collected exist in a variable named _coins or _coinsCollected. And since that information is constantly changing, like let’s say your player runs into a ghost and he steals your coins, the coin variable will change in value with the new smaller number. A running game IS variables the very core of any mechanic are variables being changed and in constant flux. Variables one can be said is the very digital bloodstream of any game.

A variable has 3 components and sometimes an optional 4th.

  1. A variable can have a public or private reference,
  2. A variable has a data type. Most common are int, float, bool, and string.
  3. A variable has a name. A good practice is for it to be descriptive.
  4. optional: a variable is sometimes given data at the time it is created.

PUBLIC AND PRIVATE REFERENCES:

A variable is declared public or private. It is private by default, but it’s good practice to type out the private part anyway.

Ex:

Public int health;

private float _speed;

Public variables: available to the public and any other class that wants to reference it.

Private variables: only available to the originating class.

SCOPE:

There’s also the issue of scope and how variables are accessed. To put it into an analogy, let’s say a public variable box ‘bike’ is placed out in the beginning of the class before any functions are created. This variable is essentially in the street and just about any class can access it, modify it, and do whatever it wants. It’s right out there in public and anyone and any class can access ‘bike’.

Now let’s say there’s another private variable called ‘ball’ and it’s in a script or class called ‘MyHouse.cs’.

this variable is located at the top of the class as well, but since it’s private, only people inside the house and the yard can access it.

Let’s say there’s a function called ‘house’, and there’s a private variable called ‘tv’ inside of it.

you can use the tv inside of the house, but if someone from the yard wants to access it, they can’t. It’s limited in scope to just what’s happening inside of the house. the tv cannot affect what’s outside of the house either. Now there’s a way for someone from the outside to see what’s happening on the tv through the use of a getter and setter, but that’s a little advanced and we’ll get to that later. Suffice to say, think of getter and setter as brother and sister who will tell you what’s happening on the show but if you want them to change the channel, you’ll have to tell setter to do that since you don’t have access to the tv.

Anyway, I hope that made sense. Back to it!

Strings:

Strings are just a set of characters strung together and the variable always begins and ends in quotes. Examples of strings are:

_PlayerName = “Warrior”;

_PlayerClass = “Barbarian”;

_NameOfSword = “Ol’ Brutus”;

Ints:

Ints are whole numbers. that’s about it. they can include 0 and negative numbers. Examples of ints are:

public int speed = 500;

private int _age = 35;

private int _donuts = 13;

Floats:

floats are numbers that have a decimal in them. numbers must also be designated with an f for them to be accepted.

examples of floats:

private float _milesPerHour = 47.89315

public float health = 80.671

private float _diameter = 214.7009

Bool:

true or false. Not much to it!

examples:

private bool _isDead;

public bool canJump;

public bool isCallingInsideTheBuilding = true;

Variables when used right make your programs easy to read and understand, even if you come back to a script years later. It’s important not to name them undescriptive things like x, or y or haHaMyGoofyVariableTryAndFigureMeOut. Good variables are things like canSwim, playerHealth, isRichEnoughToBuyThatAwesomeNewHouse. The more descriptive, the better!

--

--