Using variables

Gain a basic understanding of how variables work

Variables are a very powerful part of kode. Understanding how they work will open up many opportunities for your game. But before you start experimenting with the different types of variable you need to understand the difference between the way they are called.

There are 3 call types:


Lets look at them in turn.


When you use a variable in a brain that variable is local.

Logic cube 1

WHEN [Once]  DO [numvar:goblins][=][1]

It is only used by that brain, even if the name of that variable is used in another brain.

In this example the number variable goblins has been made to be the number 1, in another brain is this line:

Logic cube 2

WHEN [Once]  DO [numvar:goblins][incremented by][2]

In logic cube 2’s brain the variable goblins is a 2. It is not affected by the other brain. In logic cube 1’s brain the variable is still a 1. They do not interact with each other.

If a cube is destroyed the variable goes with it.


Once you have established a variable in a brain, you may want other brains to alter it. From the above example you can see that just calling the variable will not work. To affect a change in a variable in another brain you need to refer it.

Logic cube 2

WHEN [Once] DO [IWP: logic cube 1][numvar:goblins][incremented by][2]

This time the incremented by 2 will affect the logic cube 1’s brains variable. The variable will now be a 3. Logic cube 2 does not have a local variable anymore.

If logic cube 1 is destroyed then logic cube 2’s kode line will no longer run, as the variable no longer exists.


A variable that is used universally in the game and not relevant to one object can be made to be a global variable. Every time it is used, in any brain, you put a global tile in front of it.

Logic cube 1

WHEN [Once]  DO [Global][numvar:goblins][=][1]

By making it a global variable, the variable is no longer housed in any one brain.

You could destroy Logic cube 1 but still reference the Global variable from any other brain.

When to use Global, and when to use Referred

It is tempting to use Global for all your variables. For example, when I made my first game my player collected objects which added to a global variable

WHEN [Interacted] DO [Global][apples][incremented by][1]
…WHEN DO [destroy][me]

Now this worked fine because there was only one player collecting objects, and there was no multiplayer in Project Spark at that time. When multiplayer was introduced, using a Global variable will no longer work for multiple players, because you are assigning the collection of apples to a universal variable, not the player. So instead you would use a referred variable

WHEN [Interacted]  DO [objvar:player][=][it]
…WHEN DO [objvar:player][apples][incremented by][1]
…WHEN DO [destroy][me]

If the object containing the variable can be destroyed and it is vital that that variable is available to other brains then make it a Global variable.

In the above example, if the player was to be killed and destroyed the variable “apples” is destroyed as well. Even if you create another identical player, the “apples variable” is reset to 0.  The easiest way to solve that issue, without resorting to a global variable is dont destroy the player. Turn destroy at death off, and revive the player when dead instead of creating a new one. All the variables in the brain will still exist. They only reset to 0 at destroy, not death.

Comments are closed.

Website Powered by

Up ↑

%d bloggers like this: