20 GameMaker tips, tricks, and GML code snippets to use in 2021

This article has been updated since its original posting on June 20, 2018.

Did you know you can prompt a user to send a pre-written Tweet from YoYo Games' GameMaker in just a single line of code? How about this: numbers are less-precise on the HTML5 target when compared to the Windows target. Or that the YoYo Compiler target evaluates expressions and functions backwards?! This article is chock full of information about those facts and more.

Welcome to a curated list of 20 "Quick Bits", GameMaker Studio 2 tips, tricks, and code snippets that come from my Step Event Twitter account. Whether you're seasoned with the software or just getting started on your game making journey, chances are you'll learn something new.

Get the most out of your new GameMaker Studio 2 license. Here are the top 20 GML tips and code snippets for game devs in 2021.

#1 - How to reference a specific instance in GameMaker

The instance-creating functions —instance_create_layer andinstance_create_depth — are among the first functions that many new GameMaker Studio 2 developers will learn how to use. The importance of adding copies of specified objects into the game world goes without stating. But, there's more to these backbone functions than what novice game makers may realize.

How to reference a specific instance in GameMaker.

Image Transcript

instance_create_layer and instance_create_depth functions return the id of the newly-created instance. You can store this value in a variable so the particular instance can be addressed.

#2 - Macros and Enums in GameMaker

Macros and Enums do not need to be called to be initialized.

Image Transcript

Macros and Enums don't need to be called to be initialized.

An uncalled script, which houses your Macros and Enums, is enough for them to be loaded into your game.

This Quick Bit was submitted to The Step Event by https://twitter.com/designbycloud

#3 - Middle-click time saver in GameMaker

Middle-click a resource reference or function in the Code Editor. GMS2 will open the respective resource or Help Doc entry.

Image Transcript

Time saver: middle-click a resource reference or function in the Code Editor. GMS2 will open the respective resource or Help Doc entry.

#4 - Ternary Operators in GameMaker Studio 2

GMS2 supports ternary operators, an elegant approach to conditional expressions.

Image Transcript

GMS2 supports ternary operators, an elegant approach to conditional expressions.

<condition> ? <condition is true> : <condition is false>

# 5 - Hold the result of an expression in a variable

Instead of using an if-statement, you can hold the result of an expression in a variable.

Image Transcript

Instead of using an if-statement, you can hold the result of an expression in a variable.

#6 - Counter lag by using delta timing in GameMaker

Tiw movement to delta_time for consistent move speeds across all devices, regardless of frame rate.

Image Transcript

Delta timing is the number of microseconds that have passed since the last frame. Tie movement to delta_time for consistent move speeds across all devices, regardless of frame rate.

#7 - Rotate an object to face the mouse in GameMaker

How to gradually rotate an object's direction to face towards the mouse

Image Transcript

How to gradually rotate an object's direction (e.g. to face towards the mouse)

#8 - Use GameMaker's clamp instead of if-statements to limit values

Instead of using if-statements to limit the value of a variable, you can use clamp.

Image Transcript

Instead of using if-statements to limit the value of a variable, you can use clamp.

#9 - Make text and sprites blink in GameMaker

Make stuff - like text and sprites - flash every [rate] seconds using this blinking script.

Image Transcript

Make stuff - like text and sprites - flash every [rate] seconds using this blink script. Supply values for "on" and "off" states, and optionally define the rate, which will default to 1 second.

#10 - Customize your GameMaker documentation experiences

Get in the habit of writing clean code by customizing your documentation experience!

Image Transcript

Get in the habit of writing clean code by customizing your documentation experience!

In Preferences → Objects, you can setup a template that all Scripts and Events will default to.

This Quick Bit was submitted to The Step Event by https://twitter.com/designbycloud

#11 - Send Twitter tweets from your GameMaker Studio game

Twitter makes sending tweets from your game a breeze!

#12 - How to insert commas into numbers (thousands)

How to insert commas into numbers, like a score.

Image Transcript

How to insert commas into numbers, like a score (e.g. 1234567 → 1,234,567):

#13 - Logical inverses make for easy GameMaker toggles

Logical inverses make for easy toggles in GameMaker.

Image Transcript

Logical inverses make for easy toggles.

In the example below, variable soundEnabled switches between true and false each time the S key is pressed.

#14 - Keep your GameMaker Language code neat and linted

Code formatting getting messy? Copy-paste your GML into a code beautifier (linter).

Image Transcript

Code formatting getting messy? Copy-paste yoru GML into a code beautifier.

http://jsbeautifier.org works great for GML even though it's intended for JavaScript!

#15 - Nuclear Throne camera, centered between player and cursor

Making a camera that resembles the one found in Vlambeer's Nuclear Throne.

Image Transcript

Making a camera that resembles the one found in Vlambeer's Nuclear Throne.

This code snippet centers the camera, cam, between the player object, obj_player, and the cursor.

#16 - Remap a number from one range to another in GameMaker (similar to Processing.js)

Re-map a number from one range to another.

Image Transcript

Re-map a number, arg0, from one range (arg1,2) to another (arg3,4).

#17 - Incremental 45-degree rotation in GameMaker

Rotate a sprite towards the mouse in increments of 45 degrees for easy 8-directional facing.

Image Transcript

Rotate a sprite towards the mouse in increments of 45 degrees for easy 8-directional facing.

#18 - GameMaker Studio "little endian" hex color format

GameMaker stores colors in "little endian" format. Because of this, GMS2 colors are assigned $BBGGRR instead of hex #RRGGBB

#19 - GameMaker number precision can be different depending on targets

HTML5 target has less precision than Windows target. 0.5 + 0.5 does not equal 1 with floating point maths, and it may be that 1 is actually something like 1.000000000001.

Image Transcript

Numbers are actually treated as floating point values when doing comparisons in GMS. You may get different results on different platforms when checking what apepars to be an integer.

HTML5 target has less precision than Windows target. 0.5 + 0.5 does not equal 1 with floating point maths, and it may be that 1 is actually something like 1.000000000001.

To get around this, you can use the floor() or round() functions to generate a fixed integer value.

Quick Bit source, explanation, and more HTML5 differences: https://help.yoyogames.com/hc/en-us/articles/216754018-HTML5-Issues-And-Differences

#20 - GameMaker expressions and functions are evaluated differently using the YoYo Compiler

When using the YoYo Compiler targets, all expressions and functions are evaluated from left to right. On all other target platforms, they are evaluated from right to left.

Image Transcript

When using the YoYo Compiler targets, all expressions and functions are evaluated from left to right. On all other target platforms, they are evaluated from right to left.

This, for example, will give different results depending on the platform:

Thanks for reading!

Hopefully you found some of these GameMaker Studio 1.4 and 2.x code snippets, tips, and general trivia useful! You should now be able to answer the following questions that often pose game makers:

  • How do I post a tweet to Twitter using GameMaker?

    • You can post a tweet using a simple url_open_ext function call and Twitter's intent API. Refer back to #11 for the code snippet.

  • What are floating point numbers and why should I be concerned with precision?

  • Why doesn't GameMaker use standard red, green, blue hex values?

    • This is left-over from the programming language that GameMaker was originally written in, Delphi!

  • How do I add commas to my numbers?

    • Formatting your numbers, like currency, is as easy as casting integers to a string and adding a comma character every third space, starting from the end.

  • What can I do to stop my game from lagging?

    • As #6 demonstrated, you can use the delta timing functionality to help combat lag in your game.

If you're feeling like sharing your newfound knowledge, pass this post along to a friend.

Follow The Step Event on Twitter for more Quick Bits. Know something that would make a great Quick Bit? Let me know and you'll be credited!