The Basics

To begin with programming there are basically three main things you will need to understand at least a little bit about : Variables, Arrays, and Functions. As noted, these three things are all written in text files that are basically attached to your game world and game objects.

These text files are called scripts. Instead of using an application like Wordpad or Word to make these textfiles, you will usually use a programming application known as an Integrated Development Environment, or IDE. The benefits of an IDE is that they can make the process of programming much easier from auto completing key words that do things based on the programming language you are using. In some cases they also have AI to help you program directly. Super useful to say the least!

Unity comes with one already installed called Visual Studio. It is one of the most common IDE’s in use for many languages.

What Makes a Script

#pragma strict

function Start () {
    // This happens when the script starts. Good for setting things up initially.
}

function Update () {
    // This happens on every frame of the game.
}

#pragma strict is not important to understand, just important to have in the script. So keep that in there first thing.

The Start function is code that happens when the script starts...usually that’s when the scene is loaded, however there are ways to control that. For now, lets just say that this is where you set things up for your game. The script does not need the Start() and Update() functions, just that these are functions that have a particular use case.

The Update function is code that happens all the time. All of the commands are carried out as often as your computer can handle...so on a good computer this update is done many, many times a second. On a slow computer it is done less. If there are many things to do in the Update function it will take longer than if there weren’t so many things to do. So it really depends on the situation. Basically though, most of your game objects tasks will probably be done here.

You will also note that I use ‘//’ a lot in showing my examples, and even the Pseudo code previously. This is used for commenting, and the program cannot see what you write after the two slash symbols. This only applies to single lines however.

String canSeeMe = “This part can be seen”;     // But this part can not

If you want to comment out several lines of information you can use a /* and then */ :

I used the comment feature in the pseudo code section as I can simply use that to notate my code later on by moving it across or using it as a section title.

Let's look at the building blocks of programming now that we have an idea of where they go.

Variables

A Variable is like a storage unit for a piece of information. Think of them like a box that can hold information of a specific type. This variable can be set and updated as needed. In the following cases the label will be something like myInteger or myBoolean. The label can be anything you want however, but it is a good practice to use camel case to make it clear what is a variable.

thisExample, isGrounded, and character are examples of camel case.

Some will hold true or false, also known as a Boolean.

Some will hold whole numbers known as Integers.

Others will hold numbers with decimals, called Floats. Note that it has an f at the end.

Some will hold text, also known as strings.

There are many more of course, and they get more complicated and useful such as variables that hold whole gameobjects or textures.

The real power of variables comes into play when we start interacting and manipulating them. You can combine two variables, test variable contents for correct or incorrect answers, modify aspects of variables (with the more complex types like game objects) such as changing it’s colour or playing an animation, the list is nearly endless. It’s not really a question of what you can do, it’s more a question of how deep the rabbit hole goes.

By thinking forward of the sorts of variables you will need, it allows you to get into the prototyping stage faster and ‘cleaner’ as you will have processed how these variables work and if you can use variables you already have, for more than just one task.

Arrays

Arrays are similar to variables but they hold a number of variables under one name, much like a shopping list of items under the name of ‘Shopping List’. This list can then be organised and information can be gained from the array such as how many items, or variables, are in the array. Some arrays can be added to or taken from while the game is running and others are a fixed length.

Like variables, arrays are based on types. You can have an array of integers, floats, game objects or textures just to name a few.

The following is a fixed length array in the Unity game engine to use for an inventory.

I could then fill this array with the following items manually in the Unity Editor : [“Sword of Awesomeness”, “Shield of Cool”, “Health Potion of Significant Size”, “Mana Potion of Diddly Squat”].

I could then manipulate this data as I needed to. I could check if I have a health potion to use, before using it. I could apply the sword to a different array that holds what I have armed….once again, the possibilities are nearly endless.

Lists

An array that can be resized is called a List. It is less efficient than an array, as it can be resized dynamically. If you know the fixed size of the array, then use an array. If the array has to be resized, use a List.

A List is usually filled up, or populated, in script.

Functions

Functions are basically a list of commands under one name. Imagine you need to have a character hit an opponent. In pseudo code I could simply say ‘Hit Opponent’...but there are actually a lot of things that need to happen when I hit an opponent. The program has to play the animations and sound, show any effects, consider the damage to take off, take off the right amount of damage from the opponent's current health and a several other things. Over time I may also want to add even more commands to this event.

Hitting an opponent might happen from different sources, so this is a regular occurrence. Difference situations might call for different amounts of damage, or different animations...but the same basic elements will happen each time. We can put all of these commands under one ‘name’, called a Function. And then when we write that name elsewhere in the code it will do all these actions every time it is asked to do so. Asking a function to happen is calling a function. With functions we don’t have to rewrite the code over and over. We can just write it all, intelligently, once and then we can call it as many times as we like!

On top of that, it is possible to send, or pass, information known as parameters into a function when it is called. We can change how much damage is dealt, or which animation to use for example depending on where, when or how that function was called. We can do this by calling the function with the information to send along with it.

and then to call these functions we can just use the following anywhere in the same script.

Naming Standards

It is vitally important to come up with a standard naming system for your game assets and variables. This will make things easier for everybody and therefore should be stressed again.

There are certain industry standards, like variable naming and functions.

Variables start in lowercase, and uppercase for each key word afterwards.

eg. health, currentHealth, totalHealthMinusWhatOpponentJustHitMeWith

function names use upper case for each word.

eg. Damage, CheckCards, HitTheOpponentReallyHard

Although of course I don’t recommend using ridiculously long variable and function names...typing them in each time would really suck! That said, programming applications (IDE’s) make this easier with function and variable auto complete allowing you to easily select previously made variables and functions.

One of the most common errors that comes up when testing your application is spelling errors or case errors. Make sure you get into the habit of correct spelling and case management. Otherwise you will have a hell of a time while programming.

The second most common error is trying to do actions to variables that are of the wrong type. So trying to do math operations to words for example. So always consider those two things first when something has gone wrong.

Last updated