Gamemaker Studio 2 Gml

Mastering GameMaker Studio 2 GML: The Ultimate Guide to Coding in GameMaker

GameMaker Studio 2 (GMS2) has evolved dramatically over the last few years. While many users start with the visual Drag-and-Drop (DnD) system, the true power of the engine lies beneath the surface in its proprietary scripting language: GML (GameMaker Language) .

Whether you are building a pixel-art platformer, a bullet-hell shooter, or a complex RPG, understanding GML is the difference between a generic prototype and a polished, commercial release. This article is your deep dive into GML—from basic variables to advanced optimization techniques.

7. Working with Sprites & Animation

Changing Sprites:

sprite_index = spr_player_run;
image_speed = 0.2; // Frames per game step (0.2 = slow, 1 = normal)
image_index = 0;   // Reset animation

Manual Animation Control:

// Only animate if moving
if (hsp != 0 || vsp != 0) 
    sprite_index = spr_player_run;
 else 
    sprite_index = spr_player_idle;

// Sprite flipping image_xscale = sign(hsp); // Flips left/right (if hsp is -1 or 1) gamemaker studio 2 gml

Sprite Functions:

  • sprite_get_width(spr_player) - Returns width.
  • sprite_get_number(spr_explode) - Returns frame count.

Math

  • lengthdir_x(len, dir) and lengthdir_y(len, dir) – Trigonometry without the headache. Moves a point len pixels at angle dir.

Dot Syntax (Direct Access)

If you know the specific instance ID, you use a dot.

// Set the x coordinate of the player object to 100
obj_player.x = 100;

Built-in helpers

  • Movement: move_towards_point, lengthdir_x/lengthdir_y, speed/direction.
  • Collisions: place_meeting, instance_place, collision_rectangle.
  • Randomness: irandom_range, random_range, choose.
  • Timers: alarm[n] events or delta-time approaches for precise timing.

Keyboard

// Step Event
var key_left = keyboard_check(vk_left);
var key_right = keyboard_check(vk_right);
var key_jump = keyboard_check_pressed(vk_space); // true only for one frame

// Custom keys (ASCII) var key_attack = keyboard_check(ord('X')); Mastering GameMaker Studio 2 GML: The Ultimate Guide

✅ The Good

1. Incredibly Easy to Learn
GML strips away boilerplate. This code moves a player left:

if (keyboard_check(vk_left)) x -= 4;

No classes, no main loops, no imports. It’s perfect for beginners or artists-turned-coders.

2. Surprisingly Fast for 2D Games
Under the hood, GML compiles to native machine code (via YoYo Compiler 2). You can spawn thousands of objects without frame drops. For 2D platformers, shooters, RPGs, or puzzle games, performance is rarely an issue. Manual Animation Control: // Only animate if moving

3. Deep Engine Integration
GML seamlessly controls every part of GameMaker:

  • Built-in functions for collisions, pathfinding, particles, cameras, and tilemaps.
  • Direct access to sprite, object, room, and sound assets.
  • Shader support (though syntax is HLSL-like, not pure GML).

4. Quick Prototyping
You can go from an idea to a playable .exe in an afternoon. The iterative workflow (edit code → press play → test) is nearly instant.

5. Cross-Platform Export (with caveats)
GML code runs on Windows, macOS, Linux, HTML5, iOS, Android, and consoles (via partners). No massive rewrites.


Who Should Avoid It?

  • 3D game devs – GML is barely designed for 3D.
  • Programmers used to C#, Rust, or TypeScript – You’ll spend weeks unlearning habits.
  • Large teams – Lack of modules, real private variables, and tooling hurts collaboration.

Objects and instances

  • Objects define behavior; instances are runtime copies placed in rooms.
  • Use instance variables to store state per instance (e.g., hp, speed).