Fandom

Project Spark Wiki

Variables

233pages on
this wiki
Add New Page
Comment1 Share

IntroductionEdit

One of the most useful aspects of programming languages is the ability to define variables, and Kode is no exception. Basically, variables allow you to store information for future use. Every object in the game comes with lots of predefined variables, but you can create your own variables to track any extra information you want.
Some of the defined variables are read only, meaning you can see their value, but cannot change them. Some defined variables are only allow a limited range and will clamp any values not in their range.
There are a few different types of variables, and this page will try to give an example of one that already exists for each object, as well as an example of why you might want to add one of your own.

The most basic thing you can do with every variable is assign it a value. You do that with the "equals" tile.

For example:
WHEN [once] DO [number variable: strength] [equals] [5]


You need to match the different sides of the equals tile, ie. if you have a number on the left, then you need to have a number on the right. You can't (for example) tell a number variable to equal "Jim". You need a Text variable for that.

You can create a new variable of any type by going into the values section, and choosing the variable type that you'd like.

Variable TypesEdit

Ss (2014-08-26 at 07.30.36)

NumberEdit

All games are full of numbers. Some examples in each object would be things like health and maximum health, gravity factor, how high it jumps and how fast it moves. Some things you might want to add are things like mana and maximum mana, experience points and current level.

Numbers can either be whole numbers (integers) or float numbers. Check out the math tiles to see what you can do with them, but all the common things (add, subtract, multiply) as well as some fancier useful things (absolute value, round) are available.

More information on numbers.

Ss (2014-08-26 at 07.31.21)

TextEdit

Text is used for anything that people will read on the screen, but it can also be used for other things. Each page in the brain can be given a name which is a text field, as well as the object's name. You may want a text variable for things like the names of spells or special moves, or for things like speech bubbles.

Text can contain any keyboard characters: letters or numbers, as well as most common punctuation like colons, commas, etc...

You can add pieces of text together to form a bigger piece of text, which is called concatenation. For example [Text: "Hello "] [plus] [Text: "World"] would make one piece of text that contains "Hello World". You can add most variable types onto a text.

Ss (2014-08-26 at 07.30.47)

BooleanEdit

Boolean variables have two possible values, true or false. Anything that can be represented with a yes or no is a good candidate for a boolean variable. Existing boolean variables for each object are things like "on ground", "jumping", or "left mouse button" (which is global, see Scope of Variables below). You might use a boolean variable to tell if an ability has recharged.

A useful operation that booleans support is toggle. This changes the current value of the boolean to be the opposite of what it was (true becomes false, false becomes true). The lines:

When [] Do [toggle] [boolean variable: Trigger] and When[] Do [boolean variable: Trigger] [equals] [not] [boolean variable: Trigger]

do the exact same thing.

Ss (2014-08-26 at 07.30.56)

ObjectEdit

Object variables refer to objects that exist in the game. Each object has a variable named "me" which refers to itself. Objects also have a variable called "creator", which refers to the object that created it (if an object created it). You might want an object variable to track things like an object's target.

It is often useful to store an object you create in an object variable. You can say:

When [] Do [object variable: weapon] [equals] [create] [Rustic Bow]

This lets you refer to the bow you've just created later on in the brain to do things like equip it, or change the damage it deals. The important part is that now you can always refer to that specific bow.

More information on objects.

Ss (2014-08-26 at 07.31.10)

Object SetEdit

Object sets are just a group of one or more objects. Sets that every object has are its attachments, and its inventory. The built in object sets are good enough for most objects. That said, any time you want to refer to objects in a group you can add them to an object set.

Object sets start off empty and you can add items to the set by using the [increment by] tile. More generally, you can add objects and object sets together to form the object set containing all these objects. You can remove an object with the [decrement by] tile.

You can find objects in the set that match specific criteria using tiles like [objects in front], or specifying the object type. Eg. [for each of] [inventory] [prop gallery: Arrow] will loop through each arrow in your inventory.

More information on object sets.

Ss (2014-08-26 at 07.31.33)

VectorEdit

Vector variables can be used for a few different things. They are made up of 3 numbers, x, y and z, called their coordinates. They can actually be two different things: vectors or points.

A vector is defined by its direction and magnitude ("length"), and can be represented with an arrow. A point is a precise position in space. All objects keep track of a bunch of vectors like directions: up, down, left, right, forward, backward, or their velocity. They also have points like the position and the center.

Vectors are useful for pointing objects in different directions. You might also want to use them if you wanted to say shoot multiple arrows at different directions when you shoot, or always have an object at an exact offset from your character.

More information on vectors.

Ss (2014-08-26 at 07.32.29)

BrainEdit

Brain variables keep track of a brain on the current object. They allow you to access properties of the tracked brain, like the brain name, slot name or channel name, and its variables (see Brain Variable). It also allows you to control the brain by allowing you to change its current page, and know whether it is active.

If the brain referenced by the brain variable is removed from your object somehow, the brain variable will be set to nothing.

Variable couleur

ColorEdit

You can specify the colour of things you display, or the appearance of your object, and some other things. Each object has got a primary, secondary and tertiary color. You can use a color variable to change the color of a text or an icon displayed on the screen.

Ss (2014-08-26 at 07.32.06)

Boom Camera SettingsEdit

This is a special variable that keeps multiple camera settings stored: distance, target offset, pitch and yaw. Used only for boom cameras. You can change each setting individually, or use the In-World Boom Camera Editor.

Ss (2014-08-26 at 07.32.18)

Fixed Camera SettingsEdit

This is a special variable that keeps multiple camera settings stored: distance, target offset, pitch and yaw. Used only for fixed cameras. You can change each setting individually, or use the In-World Fixed Camera Editor.


Variable DefaultsEdit

We know that variables can be assigned values, but what happens if you use a variable before it is given a value? In most programming languages this would immediately give an error, but in Project Spark when you ask for a variable, if you haven't yet assigned it a value, it is given a default value. The default value is different for each variable type.

Number - Defaults to 0

Boolean - Defaults to 'false'

Text - Defaults to text without any characters in it (empty string "")

Object - Defaults to 'nothing'*

Object set - Defaults to an object set containing 'nothing'*

Vector - Defaults to the Zero vector (0, 0, 0)

Brain - Defaults to no brain**

Color - Defaults to (1, 1, 0.5) in HSL. More information on default color variable.

[*]nothing is actually a tile that you can check for. You can always ask if your [object variable] [is equal to] [nothing]. It simply means the variable isn't keeping track of an object. Asking if an object set is equal to nothing always returns false, because [nothing] is not considered as an object set in this case: to check if an object set doesn't contain any object, ask if [not] [object set].

[**]there is no specific tile you can compare a brain against to see if it is empty, but checking when [not] [brain] works. Note that a brain which doesn't contain any line of Kode is equivalent to no brain.

These defaults are especially important when looking at Evaluating when conditions.


Scope of VariablesEdit

Object ScopeEdit

Local Variable

If you do not add the [global] or [campaign] tiles or a brain in front of a variable, it is a variable with object scope. Each object has their own variables (like the speed, the position, etc). For example, if you set a number variable "number" to 1 inside the prop A with the kode [number] [equals] [1], it will not be set inside a prop B. If you want to access or modify the value of another object's variable, you need to specify the object before the variable, with the in-world picker or with an object variable (eg. [in-world picker: prop B] [number] [equals] [1]).

Remember that object variables are scoped to an object, and not to a brain. So if you remove the brain, you will still be able to access variables from the brain of other objects, and if you use multiple brains in an object, changing the value of an object variable in one of those brains will change it for every brain on the object.

Brain ScopeEdit

Project Spark allows you to add multiple brains to an object, and each brain tracks its own variables. By specifying the brain that you want before the variable name, you specify that you want to use the variable on that specific brain. This means that you can have several brains on the same object, all running kode with variables that don't interfere with each other.

For an in-depth explanation of how brain scope works, check [1].

Global ScopeEdit

Global Variable

You can add "global" in front of a variable. Such a variable is not specific to an object, it is unique in the world, and it is accessible in every object.

Even though there is no "global" tile needed in front of it, [mouse position], [all objects], or teams are good examples of global variables. If you want to make a singleplayer game, it would be a good idea to create a global "score" number variable.

Campaign ScopeEdit

Campaign variable

You can add "campaign" in front of a variable. Such a variable is not specific to an object, it is unique in the whole campaign, and it is accessible in every object from every world in that campaign.

This is only useful if you are using level linking, in order to store variables and objects that need to stay the same from one world to another. For instance, you want to store your own player as a campaign object variable to be able to create it in each new world you enter. The [unspawned objects] tile is an example of a campaign variable.


Pinning a variableEdit

You can add a variable to "My variables" in the options of a prop, which lets you quickly change its initial value at the start of the game.

In order to do that, go inside the brain editor and right click a tile of your variable, click "Info" and then "Pin".


Variable Scope in Project Spark-006:36

Variable Scope in Project Spark-0



Read NextEdit

When Conditions With Variables


Further ReadingEdit

Timing and Logic

Brain Tiles


< Back to Koding Curriculum

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.