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) {
} else {
status = "alive";
}

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.

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.

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.

Quick Comment:

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

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

Nullish: (?? / ??=)

Nullish is a quick way to check if a variable is undefined. It will act differently if the value in question has been set to undefined. The variable still needs to have been declared before this. 0 and false are still considered defined.

name = undefined;
display_name = name ?? "Dave"
IS THE SAME AS
if (name == undefined)
{
display_name = "Dave"
}
else
{
display_name = name
}

In the above example the display name will only be set to "Dave" if name hasnt been set.

name ??= default_name
IS THE SAME AS
if (name != undefined)
{
name = default_name
}

In the above example you can see it is a great way to give things a default value.

Conditional Statements Straight to Variables

Often you need an IF statement just to set the value of one variable, well this can be done in a shorthand way.

grounded = !jumping and !flying
IS THE SAME AS
if (!jumping and !flying)
{
grounded = true
}
else
{
grounded = false
}

While there is no advantage in the speed it runs at in some situations it can create cleaner code even though its slightly harder to read.

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!