Variables

Variables hold data while a player is playing a game.
There are two types of variables. Local and Global.

The name of a variable can only contain numbers, letters, or underscores.

Variables are constructed like this:

name of variable=value;

Local Variables


A local variable is a variable that is only used in one object.

If you put the variable code in the Create Event of an object, then that variable can only be used in that object.

This local variable tells how much food the player has left.

Player_Food=25;

Global Variables


A global variable can be used in any object in the game.

The only difference is that you add global before the variable name.

Example:

global.player_hp=100;

Changing the Value of a Variable


Subtracting

This code subtracts 1 from the variable global.player_hp. Notice the -=.

global.player_hp-=1;
Adding

This code adds 10 to the same variable. Notice how the -= is now +=.

global.player_hp+=10;

Drawing Variables


This code draws the health variable on the screen:

draw_text(125,75,"Health: " + string(global.player_hp));

Here is the breakdown of the code:

draw_text(x-position,y-position,"text text text" + string(the variable you want to draw));

The x-position and y-position is where you want the text to be drawn.
The "text text text" can be changed to "Health:", "Money:" or anything else you want it to be.
The "+ string(variable)" is adding the variable (which is a string) to the rest of the text.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License