The Importance of Pseudo-code
When you being programming, one of the hardest things to do is figuring out how you should code something.
Here’s a little secret: The best programmers don’t start with code.
For example, let say you want to create a piece of code that will allow your player character to fire its main weapon. How would you do that? What does computer need to do?
If you think for a moment, there is a lot involved in this simple request:
- Does the player have the main weapon selected?
- Is there ammo available to fire?
- What key does the player need to press to get there character to fire?
- Should the player just press the key once on should they hold down the key?
All these questions and more can be asked when designing a simple shooting mechanic.
A great way to solve the question of how you should implement it is by using pseudo-code.
As the name implies, pseudo-code is “fake” code. It is merely acts as a guide of our though process so that we are writing the code that we need.
Let’s use our example from above and create some pseudo-code from it.
Note how the pseudo-code is written in plain English and is written as comments. All pseudo-code should be easy to read and capture as much detail as possible so looking at the pseudo-code above is there anything we wrote that seems a bit vague?
Yes! If you guessed the last line, then you are correct!
Our last line tells us to “fire weapon”, but what does that mean? What actually happens when a weapon is fired?
Simple! Bullet appears! In game dev terms, we instantiate a bullet.
Now let’s clear that up in our pseudo-code:
Before we start writing our code, there is one more thing we need to do.
The code we are going to write will be for a function for firing our weapon. Although we need to select a weapon to fire it, the act of selecting is separate from firing so we can leave out the first line.
Our new pseudo-code will look like this:
Ok. Now let’s start coding! Let start with our function:
For our function, we’re going to pass in a GameObject called weaponType. This is because we previously stated that we wanted to be able to select our weapon before we fire it, therefore, the selected weapon will be what we pass in.
Let implement our first pseudo-code:
With this code, when we push down the Spacebar, the value of GetKeyDown will be true. (Note: GetKeyDown will evaluate true only on the first frame the key is pushed down, meaning, you can’t hold down the key to fire.)
Let’s continue:
Now we nested an additional if statement to figure out if our ammo amount is greater than 0. If this is true, we can finally create our bullet.
There we go! Our instantiate method required a GameObject, Position, and Rotation so we provide it our weapon that was passed into the function, the current position of our player (to which this script will be attached), and no rotation.
That’s it! This is the power of pseudo-code. By writing out thoughts in common English, we able to easily write out our code
So remember, next time, before you start coding…
Write. Some. Pseudo-Code!
It’ll make everything easier.