Using Variables to speed up your programming

Knowledgebase / Using Variables to speed up your programming

When you’re programming, you can use a variable as a placeholder for a value. The value of a variable can vary, since you can change it to be whatever you want. This is just like in algebra where you can use the letter x as a placeholder for a number. For example if x = 5, then x + 20 = 25.

Here’s an example of using a variable in a game: instead of setting the velocity of a spaceship to 200, you could set the velocity of the spaceship to whatever number the “Speed” variable has, and then update the “Speed” variable when you want the spaceship to go faster or slower. The image below shows a script that doesn’t use a variable (the velocity has been hard coded to 200). This script makes a spaceship move when the player presses the left or right arrow keys.

Screen Shot 2016-03-10 at 12.54.38 pm

And the image below here shows this script using a variable instead of manually setting the numbers for its velocity.

Screen Shot 2016-03-10 at 12.56.45 pm

 

The different levels of Variable usage

You can view and create variables in the Script Editor from the Variables category on the left. As you can see, variables can be global, properties, or local. 

Screen Shot 2016-03-10 at 4.46.23 pm

Variables have these 3 different levels of usage. This is to make programming less cluttered when you use a lot of different variables. Here’s how you can use the different levels of variables:

  • A global variable is stored inside the game, and can affect many game objects. For example, gravity and time are global variables.
  • A property is a variable that is stored on a single game object, and it will only affect that game object. All script blocks inside a single script can retrieve and change the value of a property. For example, health and speed are properties.
  • A local variable is stored inside a function or an event, and it will only affect that function or event. A local variable can only be retrieved and changed inside the function or event that uses it. A local variable is used for storing temporary information. For example, “the object that just collided with me” would be stored as a local variable.

 

These are the different types of variables you can use:

  • True/false (Boolean) variables are used to turn something on or off, or to set something to be true or false. For example, the visibility of something can be true or false to make it visible or invisible.
  • Number variables are used to measure something in numbers. For example, the score in a game can be 100, or 2539, or -6.
  • Text variables are are used to store messages or strings of characters. For example, a message in a game can be stored as “Welcome to my castle!”.
  • Color variables are used to store colors. For example, the background of the game can be set to red or blue.
  • List variables are are used to store multiple variables. For example, a list of all the coins in the game can be stored, and you can count the length of the list to determine how many coins there are left to collect.
  • Instance variables are used to store the identity of game objects in the game. For example, the character closest to me can be stored as an instance variable.
  • Class variables are used to store all the instances of a class. For example, when you collide with an object you can ask what the class of that object is, and if it is classed as “Hazardous” then you can inflict damage.
  • Sound variables are used to store different sounds and music. For example, when you start a level, the background music for that level will be retrieved and played.
  • Finger/pointer variables are are used to store where fingers are touching the screen. For example, if you pinch your fingers together on the screen, you can compare the distance between the finger variables and use this to determine how much to zoom in.
  • Location variables are used to store an x position and a y position together as a location (also known as a coordinate). For example, you can save the player’s location at 100 x, 500 y, and when the player dies you can make them come back to life at that saved location.

 

Creating a new Variable

You can create a new variable by dragging out one of the “Set variable” blocks from the Variables category in the Script Editor.

variables1

Click on the “i” and select New variable. This will create a new variable and ask you to name it.

variables2

Once you’ve named the variable, select what type you want it to be by clicking on true/false. In this example, my Speed variable is a number.

 

Using your variable

To use a variable that you’ve created, find its variable block underneath its “Set variable” block, and drag it inside another block where you’d normally set the value.

use-variable

 

Now I can use this variable instead of manually typing in the number when I want to change the velocity of my spaceship. Pretty handy!

You can go on to use these variables in even more interesting ways by making them public variables which will be editable on a game object’s instance properties. This allows you to create different kinds of the same object, for example, you might want to have multiple coins you can collect, but some of them might give you more points.