GML Code Reference

All the info about coding in GameMaker you'll need to get started

🧑‍💻

Written by Josh Humphriss

🗓️

Posted: 02 Oct 2024

🗓️

Modified: 23 Oct 2024

⏱️

5 min read

👁️

107 views

Introduction

Want to start coding in GameMaker? This is a great place to start. This article will include an overview of the language, plus a few functions I have picked out as being particularly important for beginners. If you want to see the full documentation for everything, check out the official manual.

This was created primarily to assist the GameMaker tutorial as part of Warwick Game Design. This page will include all of the functions that you'll need to use as part of the tutorial.

Jump to:

Basic Syntax

Most of this is pretty similar to other languages like JavaScript, however it's worth skimming through as there are some differences. If you don't yet know how to code, I would recommend finding a proper tutorial - this is aimed to be skim read by someone who is already familiar with coding in other languages.

Variables, Functions & Hello World

Variables do not need to be declared, only assigned to (like Python). In the following code snippet, the variable "hello" is not yet declared.

hello = "Hello, World!";
show_debug_message(hello);

However, you may declare variables with the var keyword if you want to. This will restrict their scope to the current event that you're working in.

var local_variable = "This is deleted after the script finishes running";

Global variables can be defined by writing global.variable, although use this sparingly (if at all).

Functions are defined like this:

function name(parameter1, optional_parameter = default_value, ...) {
   x = 2
}
// x does not exist here

...or like this:

name = function(parameter1, optional_parameter = default_value, ...) { /* code */ }

Note that we do not need to specify the data types of the parameters - it is dynamically typed. Also, we saw in the above example that variables do have scope. However, there is no support for public/private functions or variables etc. so everything is public.

Data Types

GML Code supports the following data types:

Data TypeExamplesNotes
Real Numbers52, 0, -12.2Don't worry what type of number you have, the compiler will sort it out (e.g. if you type 12.0, it will actually be stored as the integer 12).
Booleantrue, falseNote that when interpreting real numbers as booleans, <=0.5 is considered false and >0.5 is considered true.
Strings"chickens"You may not use single quotes to denote a string. If you start a string with @ like this: @"Hello", it may span over multiple lines. Concatenation is done with +.
Arrays[1, 4, "hi"]Arrays used to be passed by value, but now they are passed by reference. You can initialise arrays by writing arr[4] = 2 before defining arr. Length: array_length(arr).
Structs{key: value}A struct is like an object without any methods. This behaves like python dictionaries, or JSON.

There are other data types, including int64, hex literals, handles, binary literals, NaN, infinity, undefined, enums and a few more, however you don't need to know about these to get started. Click here if you're interested.

Conditionals

If statements look like this:

if (condition) {
   doSomething();
} else if (condition2) {
   doSomethingElse();
} else {
   doSomethingEvenDifferent();
}

Logical operators:

&&and
||or
!not

Semicolons & Comments

You may put semicolons at the end of statements, however this is entirely optional - a new line break is enough to denote the end of a statement.

Comments look like this:

// single-line comment

/* multi-line
comment */

However, there's also a few special comments you might want to be aware of that are displayed in the editor in some way. You can use #region tags to mark regions in the code that can be collapsed in the editor. These can have comments e.g. "#region Setting up variables" then "#endregion". Comments beginning with /// can be put at the top of the file, and contain a description to show in the Object Editor.

User Input

Functions:

keyboard_check(key)Checks if the given key is currently held down
keyboard_check_pressed(key)Checks if the given key was pressed in the last frame
ord(char)Gets the key e.g. ord("A") for the A button.
mouse_check_button(mb)Checks if the given mouse button is currently held down
mouse_check_button_pressed(mb)Checks if the given mouse button was pressed in the last frame

Variables:

vk_left, vk_up, vk_enter, vk_escape etc.Keys that don't type things have special values
mb_left, mb_middle, mb_right, mb_any etc.Values for mouse buttons

If you'd like to support controller input, there's some more info here.

Movement & Sprites

Functions:

motion_add(direction, magnitude)Adds motion to the current object.
move_wrap(bool horizontal, bool vertical, margin)Enables movement wrapping around the edge of the screen.

Properties of Objects:

image_angleThe rotation of the sprite of the current object in degrees
sprite_indexThe sprite to apply to this object.
speedThe speed of the object in pixels per frame
directionThe direction the object is moving in, independent of image_angle
xx-coordinate of current position
yy-coordinate of current position

Check out this page for more info.

Collisions

Functions:

move_and_collide(x_speed, y_speed, collide_with)Handles all movement and collisions with given object(s).
place_meeting(x, y, object)Detects if there is an instance of the object at the position.

Creating & Destroying Instances

Functions:

instance_create_layer(x, y, layer, object)Creates an instance of object on the given layer at the given position.
instance__destroy()Destroys the current instance.
instance__destroy(obj_or_inst)Destroys the given instance, or all instances of that object.
instance__copy(fire_create_event)Copy this instance. You can choose if the Create event should run.
instance_number(object)Counts the number of instances of this object.

Layers:

"Instances"The normal layer for things.
"Background"Here you can add things to display on the background.
You can also add your own layers!

Rooms & Window Management

Functions:

room_restart()Restarts the current room to its initial state.
game_restart()Restarts the whole game.
room_goto_next()Goes to the next room (useful if you have multiple levels).
window_set_size(width, height)Sets the size of the window.

Maths & Numbers

Functions & Operators:

random(max)Generates a random number between 0 and max
random_range(min, max)Generates a random number between min and max
choose(val0, val1, ...)Outputs a random value from the inputs
floor(n)Round a number down (see also: ceil, round)
n++Equivalent to n = n + 1
n mod 2Gets remainder of division (see also: div)
lerp(a, b, amt)Liner interpolation, e.g. lerp(5, 10, 0.5) = 7.5.
clamp(val, min, max)Ensures a value stays between min and max.
power(x, n)Does x^n.

Variables:

current_timeNumber of milliseconds since the game has started
delta_timeNumber of milliseconds between this frame and the last

Sound & Particles

Functions:

effect_create_above(effect, x, y, size, colour)Creates a particle effect
audio_play_sound(sound, priority, loop)Plays a sound.

Physics

If you'd like to implement more advanced physics, see this page. I don't think I'd be contributing much by copying these functions over here. Note that you shouldn't need to use this physics system for most simple games.

Miscellaneous

Functions:

draw_text(x, y, text)Draws text onscreen

Properties of Instances:

alarmAn array containing the time until each alarm will trigger.