Variables and writing pseudocode

Daniel Kirwan
3 min readMar 25, 2021

--

What are variables are what do they do?

They are the building blocks to every game being made. Variables are a place to store data for use later on and for games that can be anything from the player's health, score, or ammo count. None of those exists when making a game and has to be created by the programmer, and the programmer sets who can have access to the variables for manipulation.

Private or public?

You can set your variables to private or public but what does that mean? Private means that only the script your code is in can access that variable and public means that other scripts can access that variable. You can also use [SerializeField] which when set to a private variable will make the variable available in the Unity editor. Why would you do that? Well, it is generally used so that level designers can make small changes to variables to make the game run better without having to make changes to the code.

Global or Local?

You have a choice when programming to make your variables global or local. Global variables are available to all methods/functions in a script but local variables are only available in the method/function they appear in. This is called scope. Deciding on where your variables go can be very important as it is bad practice to have a global variable when a local one would suffice.

Writing Pseudocode

Pseudocode is great for breaking down what you’re working on to get a better understanding of what you need to do and give yourself workable chunks. This is also a great way for other members of the team to give you a breakdown in plain language of what they need before you run off and start programming.

For instance, in my current project, I want to instantiate an enemy game object when the game starts and have it have a certain position. So, I might write it out as follows:

And now that I have written it down like this I have a better understanding of what I actually need to implement in the game.

Above you can see that I have created a local variable for the spawn position then the GameObject that I want to spawn is created and I now have the position that I want to spawn.

Next is moving the enemy in the way that I need. For the project, I will be just moving the enemy down along the Y-axis for now.

That is it for this article on variables and pseudocode, come back for more articles.

--

--