Macros in GameMaker are essentially constants. They are a way to define values that will not change throughout the execution of the game. Defining a macro happens at compile time, and the value associated with a macro name gets directly inserted into the code wherever it is used.
In GameMaker Studio 2, you create macros using the
#macro directive, like so:
After these macros have been defined, you can use
SCREEN_HEIGHT anywhere in your code, and GameMaker will treat them as if you had literally typed
During compilation, GameMaker will replace
768, so the compiled code will be equivalent to:
This is especially useful for values that are used frequently throughout your project and may need to be updated or changed. Instead of searching through your entire codebase to replace a value, you only need to update the macro definition.
Additionally, using macros can make your code more readable and maintainable, since names like
SCREEN_WIDTH can provide more context than a raw number, making it clear what the purpose of the value is.