GameMaker: code architecture¶
How to structure objects, events and GML code so that the game stays easy to change: instances and event order, global state, functions, structs and types, async events, state machines, UI and multiplayer. Engine-independent patterns, such as components, fixed time steps and authoritative servers, are in Game development.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 8 Game Programming Patterns (Robert Nystrom): engine-independent, free online, and the background to the state machine and component rules.
- 7 GML Code Overview (GameMaker Manual): variables and scope, script functions and methods, structs and constructors, and evaluation order.
- 7 Object Events and Event Order (GameMaker Manual): what each event is for, and which orders GameMaker guarantees.
Key rules¶
Objects, events and instances¶
- 8
GAMEMAKER-CODE-1Don't rely on the order in which instances run the same event, or on the order in which a room creates its instances. GameMaker guarantees only the order of the events, such as Begin Step, then Step, then End Step, so move code that must follow another instance's Step event into End Step. A room creates its instances one at a time in its instance creation order, so awithin a Create event runs for none of the instances that don't exist yet. (GameMaker Manual, Event Order, with, Room Properties: instance creation order) - 7
GAMEMAKER-CODE-2Declare every instance variable in the Create event, or as an Object Variable, and usevarfor values that only one event needs. Otherwise an event that runs before the one that first sets the variable, such as Begin Step before Step, fails when it reads it. Feather reports this asGM2016. (GameMaker Manual, Feather Messages: GM2016) - 7
GAMEMAKER-CODE-5Keep game-wide state in a few known places: declare every global variable in one script function or one controller object at the start of the game, always with theglobal.prefix, and define all macros in a dedicated script asset. Global variables last until the game ends and are easy to confuse with instance variables of the same name, andglobalvaris deprecated. Macros can't change at run time, but can take a different value in each configuration. (GameMaker Manual, Global Variables, Constants) - 6
GAMEMAKER-CODE-3Refer to a particular instance through its instance ID, and use the object name only in code meant for all its instances. When several instances exist,obj_enemy.hpreads the first one created, which may change, whilewith (obj_enemy)and functions such asinstance_destroy(obj_enemy)act on every instance of the object and of its children. (GameMaker Manual, Objects vs. Instances, with) - 6
GAMEMAKER-CODE-4Put behaviour that a family of objects shares in a parent object that is never placed in a room itself, and callevent_inherited()in a child's event when the parent's code for that event should also run. A child event without that call replaces the parent's. Keep hierarchies shallow and compose the rest (GAMEDEV-2). (GameMaker Manual, Parent Objects, Game development: components) - 6
GAMEMAKER-CODE-6Make an object persistent only if its instance must outlive the room, such as a game manager or the player, and create that instance exactly once. A persistent instance runs its Create event only once and goes on into every following room until you destroy it, and one that is still deactivated when the room changes is lost. (GameMaker Manual, The Object Editor: persistent)
Functions, structs and types¶
- 7
GAMEMAKER-CODE-7Define global functions in script assets withfunction name() {}, and functions inside objects and constructors as method variables,name = function() {}, so that children can override them. The compiler optimises calls to script functions, so a script function that a child object defines doesn't override its parent's. To call a script function through a variable every step, convert it once withmethod(undefined, fn), because each indirect call otherwise creates a new method. (GameMaker Manual, Script Functions vs. Methods, Script Functions And Variables) - 7
GAMEMAKER-CODE-8Use constructors and structs, not object instances, for data and logic that need no events, drawing or collisions, such as stats, inventories or states, and declare their methodsstaticso that all structs from a constructor share one copy. Structs have no processing overhead after they're created and are garbage-collected, but their methods run only when your code calls them. (GameMaker Manual, Structs & Constructors compared with objects, Static Variables, Garbage Collection) - 6
GAMEMAKER-CODE-9Don't depend on the order in which function arguments or parts of an expression are evaluated: call functions with side effects, such asbuffer_read, in separate statements, and use brackets in expressions. The order differs between targets, for example between Windows and HTML5. Feather reports several function calls in one argument list asGM2023. (GameMaker Manual, Evaluation Order, Feather Messages: GM2023) - 6
GAMEMAKER-CODE-10Document script functions and constructors with JSDoc comments that give the types of@paramand@return, and let Feather check types. Feather uses the comments for code completion and to check calls, and Strict Type mode makes the checks stricter. (GameMaker Manual, JSDoc Comments, Feather Data Types, Feather Preferences)
Async, state, UI and multiplayer¶
- 7
GAMEMAKER-CODE-11Use the asynchronous versions of slow operations, such as HTTP requests, file loading and adding sprites, and in the Async event check thatasync_load[? "id"]matches the request ID you stored. Async events run in every instance that has them, andasync_loadis valid only inside the event. (GameMaker Manual, Async Events, http_get) - 5
GAMEMAKER-CODE-12Model objects with distinct modes, such as enemies, the player or menu buttons, as finite state machines: one state variable, states named by an enum, and the state code that a family shares in its parent object. When states grow complex, make each state a struct with its own methods (the State pattern). This appliesGAMEDEV-21. (GameMaker, Finite State Machines (GML), Nystrom, State) - 5
GAMEMAKER-CODE-13Build menus and the HUD in UI layers, with Flex Panels so that the layout adapts to the display, and place the objects that handle their input in those layers. UI layers are shared by every room and draw on the GUI layer or a viewport, where cameras don't move them, and Mouse events andmouse_xandmouse_yof objects on them use UI coordinates. (GameMaker Manual, UI Layers, UI Layers At Runtime) - 5
GAMEMAKER-CODE-14Choose the multiplayer approach before building gameplay: Rollback Multiplayer plays online only on the GX.games target and shares only input, so every client must run the same logic, and for other platforms use the Photon extension or your own server with the networking functions. This appliesGAMEDEV-5andGAMEDEV-9. (GameMaker Manual, Rollback Multiplayer, Rollback System, Networking, YoYo Games, GMEXT-Photon)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 Feather messages: the code editor reports
GM2016for instance variables declared outside the Create event,GM2023for several function calls in one argument list, andGM2040forevent_inherited()in an object whose parent has no such event. Set each message's severity in the Feather Preferences. Feather directives with a path, kept in a script such asFeatherConfig, set the profile and turn messages off or back on for the whole project, and are committed with it. ChecksGAMEMAKER-CODE-2, and partlyGAMEMAKER-CODE-4andGAMEMAKER-CODE-9. - 7 Feather type checking: the TypeErrors profile and Strict Type mode check calls and assignments against the types in JSDoc comments, and a directive with a path, such as
// Feather use type-errors in /*, sets the profile for the whole project. Partly checksGAMEMAKER-CODE-10.