Beyond these syntax changes, GameMaker Studio 2 introduced revolutionary concepts that redefined its capabilities. The arrival of and Constructors gave developers Object-Oriented Programming (OOP) capabilities, allowing for the creation of complex data structures that mirror real-world objects. Under the hood, updates like the shift from DX9 to DX11 in the Windows Runner opened doors for superior graphics and performance, setting the stage for more visually ambitious 2D games.
This guide provides a deep dive into GML, exploring its fundamentals, advanced features, and the best practices that will set your projects up for success.
move_speed = 4; sprite_idle = spr_player_idle; sprite_walk = spr_player_walk;
Adopt a consistent programming style to make your code readable for others and for your future self. Use clear indentation (four spaces is typical), meaningful variable names, and comment your complex logic. The script editor supports #region and #endregion tags to fold sections, which is particularly helpful for large scripts containing multiple functions.
Curiosity getting the better of her, Elara whispered a forbidden script: instance_create_layer(x, y, "Instances", obj_ancient_one); .
// Array inventory = ["sword", "shield", "potion"];
You can check the data type of any variable using the typeof function, and convert values as needed using the various is_*() and conversion functions.
// We define 4 points. // Points 1 & 2 are on the "Bottom" face (pushed back). // Points 3 & 4 are on the "Top" face (at position).
resource to create a new object. This acts as the container for your code. Assigning Sprites
if (hp <= 0) instance_destroy(); else if (hp < 20) sprite_index = spr_player_wounded; else sprite_index = spr_player_idle; Use code with caution.
var total = baseDamage * multiplier; return total;
In GML, code is written in a syntax that is similar to other programming languages. It uses a combination of commands, functions, and variables to create game logic. Some basic data types in GML include:
Both the built-in runtime functions and your custom user-defined functions can resolve expressions and return values, giving you complete control over your game's logic.
Do not place heavy calculations in the Step event if they don't need to run every frame.
2D matrices. Ideal for pathfinding matrices, strategy maps, or inventories.
GML is a high-level, object-oriented scripting language that is specifically designed for game development. It was created by Mark Overmars, the founder of GameMaker, and has since become the de facto standard for game development in GameMaker Studio 2. GML is used to create game logic, AI, and interactions, making it an essential tool for game developers.
Maintaining a consistent is vital. Whether you prefer 4-space indents or tabs, putting opening braces on new lines, or naming local variables with an underscore ( _local_var ), consistency is key to making your code readable to others (and your future self). For developers using external IDEs like VS Code, extensions like the GML Language Support can provide enhanced syntax highlighting, IntelliSense, and context recognition, making it easier to adhere to these stylistic standards.
Here's an example of a GML function:
