You can declare custom functions directly in script assets or assign functions to variables inside objects using the function keyword:
To start coding in GameMaker Studio 2, you need to understand a few basic building blocks. These blocks make up every game.
Getting started with GML requires understanding its syntax rules and structure. Variables and Scope
GameMaker Language strikes a masterclass balance between user friendliness and development depth. By moving away from visual scripts and diving into variables, constructors, structs, and custom functions, you unlock the ability to craft fast, complex, and professional-grade desktop, mobile, and console games.
As your project grows, you will need to leverage GameMaker's modern programming structures. Lightweight Structs and Methods gamemaker studio 2 gml
: Handles visual rendering. If you write GML in the Draw Event, it overrides GameMaker’s automatic sprite drawing, meaning you must manually call draw_self(); to display the assigned sprite.
: Runs every single frame of the game (usually 60 times per second). This is where you put movement inputs, collision checks, and state updates.
Right-click in the Asset Browser, select to create a script asset. In the editor, you can write your function:
Runs when an object is destroyed or the room changes. Critical for destroying data structures and preventing memory leaks. 4. Modern GML Features (GameMaker 2.3+) You can declare custom functions directly in script
Before diving into the code, it's crucial to understand that GML is primarily an event-driven language. This means your code is not a single, linear script. Instead, it is organized into blocks attached to specific within an Object . For example, you might write movement code in a "Step" event that runs every frame, or collision code in a "Collision" event that triggers when two instances meet.
GML in GameMaker Studio 2 offers a fast, focused path to building 2D games. By combining event-driven design, modern GML features (functions, structs, enums), and solid engineering practices (state machines, pooling, modular code), you can create performant, maintainable games—from prototypes to commercial releases.
Runs every frame after the Step Event. It handles rendering sprites, text, and visual effects. Writing non-visual logic here drastically lowers performance.
// Defining a struct var _weapon = name: "Iron Sword", damage: 15, durability: 100 ; // Accessing struct data show_debug_message(_weapon.name); Use code with caution. Methods and Constructor Functions Variables and Scope GameMaker Language strikes a masterclass
The Draw Event: By default, GameMaker handles drawing sprites. However, if you want to create custom health bars, lighting effects, or UI elements, you use GML in the Draw event to take manual control of the screen. Essential GML Concepts for Beginners
GML is GameMaker’s native scripting language designed specifically for 2D game development. It mixes C-like syntax with engine-specific functions and built-in variables, allowing rapid iteration on gameplay, physics, UI, and more. GML is lightweight but expressive, making it well suited for prototypes and full commercial projects alike.
// Creating (declaring) variables myNumber = 10; // A real number myString = "Hello World"; // A string of text myBool = true; // A boolean (true or false) myArray = [1, 2, 3]; // An array
To start coding with GML, you’ll typically work with these fundamental elements:
// Creating and manipulating an array inventory = ["Sword", "Shield", "Potion"]; array_push(inventory, "Elixir"); Use code with caution. DS Maps (Dictionaries)
Use switch statements to handle different player states like "idle," "running," or "attacking".