gml_pragma to Boost Your Game’s Performance

In this tutorial, I will detail how you can increase performance in your games by using gml_pragma function. If you have knowledge about this function, you can skip the first part and continue with second one.

What is gml_pragma?

It’s a function dedicated for intervening the compiling process. To explain it easier, this function affects when you run or create an executable of your game, not when you actually open the game. This means that your game is “created” with them. With the following functions, you’ll understand them better.

How to use gml_pragma?

It is generally put in Game Start Event with some exceptions(see forceinline below). You provide the pragma command and arguments if requested. Here’s the total pragma commands of Game Maker Studio 2.

  • forceinline
  • global
  • PNGCrush
  • Texgroup.Scale
  • UnityBuild

forceinline Pragma

Simple explanation : It compiles the scripts you want inlined, not referenced. What it means is that your scritps are actually copied to wherever you use them. Here one example: Think that you have a script named scr_calculate

///scr_calculate();
global.var += 6867970;
gml_pragma("forceinline");
//This script will be inlined.

Now, think that you have an object and you call this script in 4 places. Normally, without inline your code looks like this:

//MY OBJECT'S STEP EVENT

scr_calculate();

///... do some stuff here...

scr_calculate();

///... do some stuff here...

scr_calculate();

Now when you compile your game with forceinline pragma, your final code actually becomes like this:

//MY OBJECT'S STEP EVENT

///scr_calculate();
global.var += 6867970;
gml_pragma("forceinline");
//This script will be inlined.

///... do some stuff here...

///scr_calculate();
global.var += 6867970;
gml_pragma("forceinline");
//This script will be inlined.

///... do some stuff here...

///scr_calculate();
global.var += 6867970;
gml_pragma("forceinline");
//This script will be inlined.

This is useful and powerful in many cases. The only downside is that it naturally increases the final size of your application. But definitely worth it. Leave a comment for more detailed discussion 🙂

It only works in YYC builds.

global Pragma

This pragma is used for executing a script before opening the game. It saves a lot of processing power when used properly. Any script that’s provided as the argument is executed before the game.
You cannot create instances or something that needs a room as the script is not executed in a room.

gml_pragma("global","scr_create_10000words_database()");
//Executes the script that creates 10.000 Words. It's an expensive task
//to do on runtime. Saves me so much time!

Write this in a Game Start Event

PNGCrush Pragma

Man, that’s perhaps the best in here! This pragma executes a program called PNGCrush when you build/execute your game. What it does is simply optimizes and reduces the size of the textures pages(sprites, tiles etc.) in your game. It decreases your game’s final size drastically. I use this in all my games.

gml_pragma("PNGCrush");
//Optimizes all the graphics in the game(Reduces size therefore your application's final size is decreased)

Write the function in Game Start Event

Texgroup.Scale Pragma

This function scales down your texture files on compile time. It needs two arguments.

gml_pragma("Texgroup.Scale", "mainMenuTexturePage", "2");
//Half all the sprites/tiles etc. in mainMenuTexturePage.

UnityBuild Pragma

This one saves a lot of time when used properly. It first collects all your scripts, codes, events all your GML code into a single and then builds everything. The benefit in using this pragma is that you can get really fast builds. The disadvantage of it is that if you change a code in your game, then the compiler has to recollect and build all your code again. This is useful when you only need to change graphic or sound files in your game.

It only works in YYC builds.

gml_pragma("UnityBuild", "true");
//Or "false" to disable.

Write this in a Game Start Event

Share

Marty

Truely speaking, I don't know what am I doing most of the time.

Leave a Reply