Dragonfly

Question: What is a macro in GameMaker?

Answer

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:

#macro SCREEN_WIDTH 1024
#macro SCREEN_HEIGHT 768

After these macros have been defined, you can use SCREEN_WIDTH and SCREEN_HEIGHT anywhere in your code, and GameMaker will treat them as if you had literally typed 1024 and 768.

For instance:

// Set the size of the application surface
surface_resize(application_surface, SCREEN_WIDTH, SCREEN_HEIGHT);

During compilation, GameMaker will replace SCREEN_WIDTH with 1024 and SCREEN_HEIGHT with 768, so the compiled code will be equivalent to:

surface_resize(application_surface, 1024, 768);

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.

Was this content helpful?

Help us improve by giving us your feedback.

Other Common Game Engines Questions (and Answers)

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

AWS system design ebook

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost