Tips and Tricks for Programming in GameMaker

Toggle Variable:

Using ! we can ask for NOT that value, so a 1 will return 0 and a 0 will return 1. We can use this to flip between 0 and 1 to toggle things on and off.

toggle = !toggle; // 0
toggle = !toggle; // 1
toggle = !toggle; // 0
toggle = !toggle; // 1
toggle = !toggle; // 0

Is the same as:

if (toggle == 0) {
	toggle = 1
} else {
	toggle = 0
}

Ternary IF / Shorthand IF:

Using ? tells GameMaker that you are about to put a conditional statement inline. You will use : to signify what to return for both the true and the false parts of the statement just like you would use ELSE, however it is mandatory to have an expression for both the true and false.

status = hp < 0 ? "dead" : "alive";

Is the same as:

if (hp < 0) {
	status = "dead";
} else {
	status = "alive";
}
GameMaker short hand IF
Incrementing and Decrementing:

You can use ++ or -- to add or subtract 1 from a number.

i++;

Is the same as:

i=i+1;

You can even read and update simultaneously:

var i = 0;
arr[i++] = "Zero";
arr[i++] = "One";
arr[i++] = "Two";

++i can also be used if you want to first add one to the number and then output it. Someone try ++i++ for me, I don’t see any reason why that wouldn’t work.


Smooth Movement to Point:
x += (mouse_x - x) * 0.1

Rather than having things instantly teleport to a location you can use this code to make them travel 10% of the distance each frame, this means as they get closer they move slower giving a more natural smooth movement.

GameMaker Smooth Movement
Array Literals:

When you initialise an array you can do this:

arr = ["First","Second","Third"];

Is the same as:

arr[0] = "First"; 
arr[1] = "Second";
arr[2] = "Third";

You can also do this to have an empty array:

arr = [];

Duplicating Lines with Ctrl+D:

Ctrl+D will either duplicate the selected line right below where the cursor is, or if you have an amount of text selected it will duplicate that next to where you are currently selected.

GameMaker Dupliate Lines with Ctrl+D
Edit Multiple Lines with Alt:

You can actually place multiple cursors by clicking while the Alt button is selected, any inputs from this point will update all the cursors simultaneously.

GameMaker Multiple Line Edit with Alt
Quick Comment:

With Ctrl+K you can instantly comment out a selection of code so it doesn’t execute.

GameMaker Easy Comment Ctrl+K
Find the size of an enum in GameMaker:
enum item_list {
    item1,
    item2,
    item3,
    size // this will be 3
}

If you make the last value of an enumerator contain its length it is really easy to get in the future by just doing item_list.size



This is a living document and I will add more as I think of them, if you have any ideas you think should be added contact me!

About the Article:
Easy Difficulty
GameMaker
By David Strachan