Macros in Gamemaker Studio 2: HOW and WHY

Today you will be learning about Macros, their basic usage, and a really cool way of using them that will save you a tremendous amount of time and hassle.

If you’re not familiar with macros, you can simply make them by typing a pound sign (#), followed by “macro.” Type a space, enter the name (ex. my_macro), then put anything after it. You can put a string, a number, a function, or even an array!

Basic macro structure
Gamemaker Studio 2 macro structure

Once you initialize a macro, note that you cannot change it afterwards. A macro is a constant, meaning once you type them, the value that they’re set to are what they are forever. Right away, you can probably see that macros are extremely versatile, and how they can save you a lot of time and effort whilst coding. For example, instead of having to type draw_set_color(c_white) after every draw event to reset the color palette, you can create a macro instead to do it with less code:

#macro resetCols draw_set_color(c_white)

Now, whenever you need to reset the palette, you can just type “resetCols” instead of “draw_set_color(c_white).” That’s a 60% text savings! Think of how much cleaner your code can be with these helpful macros. Here’s a list we’ve compiled of some helpful macros:

Macro What it Does
#macro camX camera_get_view_x(view_camera[0]) Returns the camera’s current x position
#macro camY camera_get_view_y(view_camera[0]) Returns the camera’s current y position
#macro aspectRatio (display_get_width() / display_get_height()) Returns the display’s aspect ratio
#macro gMouseX device_mouse_x_to_gui(0) Returns the x position of the mouse on the device in relation to the GUI layer
#macro gMouseY device_mouse_y_to_gui(0) Returns the y position of the mouse on the device in relation to the GUI layer
#macro equals == Counts as the “double equals.” This can make “if” statements easier to read
#macro camW camera_get_view_width(view_camera[0]) Returns the view camera’s width
#macro camH camera_get_view_height(view_camera[0]) Returns the view camera’s height

So, now that we’ve covered the basics of macros, it’s time to learn about the more advanced usage of them. This method can save you a lot of time and hassle, as well as making your code a lot cleaner! Instead of thinking of macros as specific “buckets” of data, what if we thought of them more as pointers? I’ll show you what I mean. Look at this code:

function reset_enemies()
    for(var i = 0; i < enemyCount; i++)
        enemies[i].x = random_range(room_width, room_height);
        enemies[i].y = random_range(room_width, room_height);
        enemies[i].direction = random_range(0, 360);
        enemies[i].speed = random_range(1, 5);

This right here, enemies, is actually a macro. “But hold up”, you might say, “macros are constants, you can’t set them!” And yes, macros are indeed constants- they still are. If we go to where the macro is initialized, we can see that it’s pointing to a global variable, and that global variable is an array.

// Baddies
global.enemiesList[0] = obj_mean_cat;
global.enemiesList[1] = obj_angry_dog;
global.enemiesList[2] = obj_menacing_hampster;
global.enemiesList[3] = obj_bob_ross;

#macro enemies global.enemiesList
#macro enemyCount array_length_1d(enemies)

This means you create an array of any kind, then access that array with a much much shorter length of text compared to if you’d be doing it without macros. In addition, if you later decide to change the name of your array, you won’t have to go through changing each variable to the new one- it’ll all be changed if you change that one macro.

This nifty tip will save you a lot of typing time and hassle- it will make your code considerably cleaner as well! Hopefully this article has been useful to you, and if not, please do leave a comment below, and I’ll try to help you out! :)



(Comments'd go here..)