gml_pragma

The gml_pragma function affects how the given target compiles your code and should be called with the different commands to further optimise the final compilation of your project. These commands are effectively pre-processed before the game is compiled and so the function can be placed anywhere in your project and it will still be processed before the game is fully compiled. The available commands are as follows:

NOTE The first argument to the gml_pragma function must be a compile-time string constant and not a variable.

Compiler OptimisationsCompiler Optimisations

Compiler optimisations can be provided with the "optimise" (or "optimize") pragma. Their basic syntax is: 

gml_pragma("optimise", "<specific_optimisation>", "<control>");

"<specific_optimisation>" is a string containing the specific optimisation to modify.

"<control>" is a string that contains a comma-delimited list of commands that control the optimisation from this point in your code: 

The following table lists all optimisations that you can use: 

Compiler Optimisations
OptimisationDescription
js_array_checkIf ON then the compiled code will include checks that variables are arrays and do basic error checking on the array, omitted if OFF. These checks are done at each use of the array.
NOTE If OFF then you need to make sure that every variable used as an array is definitely of type array before using it.
js_error_checkIf ON then the compiled code will include checks in the generated JavaScript code to bring it in line with how the VM/YYC runner handles errors. These checks will be omitted if OFF.
NOTE This can be safely omitted if your code is not erroring when used, generally something that can be disabled once your code is working error free.
js_check_indexIf ON then the generated code will include checks on the index on array access (read and write) to catch out of range errors. If OFF you need to make sure to always use an array index that's within the array's length (i.e. from 0 to array_length-1).
js_pre_post_no_longIf ON then increment ++ and decrement -- expressions are not checked to see if they operate on values of type int64 (or Long in JavaScript). You need to make sure that no variables are of type int64 when using the ++ and -- operators.
js_use_infix_opsIf ON then JavaScript's binary operators +, -, *, etc. are used in preference to using GameMaker's type checking functions. In this case you need to make sure that all variables used in the expression are of type real.

Example

As an example, to optimise the following function: 

function multiples_of_two()
{
    var a = [];
    for (var i = 0; i < 100; i++)
    {
        a[i] = i * 2;
    }
}

You would then add gml_pragma statements as follows: 

function multiples_of_two()
{
    gml_pragma( "optimise", "js_array_check", "push, off");
    gml_pragma( "optimise", "js_error_check", "push, off");
    gml_pragma( "optimise", "js_check_index", "push, off");
    gml_pragma( "optimise", "js_pre_post_no_long", "push, on");
    gml_pragma( "optimise", "js_use_infix_ops", "push, on");
    var a = [];
    for(var i = 0; i < 100; i++)
    {
        a[i] = i * 2;
    }
    gml_pragma( "optimise", "js_use_infix_ops", "pop" );
    gml_pragma( "optimise", "js_pre_post_no_long", "pop" );
    gml_pragma( "optimise", "js_check_index", "pop" );
    gml_pragma( "optimise", "js_error_check", "pop" );
    gml_pragma( "optimise", "js_array_check", "pop" );
}

Usage Notes

 

 

Syntax:

gml_pragma(command, [optional...]);

ArgumentTypeDescription
commandStringA string with one of the commands listed above.
[optional]StringSome of the available commands require an optional argument or arguments. These are explained above for each command.

 

Returns:

N/A

 

Example:

gml_pragma("forceinline");

The above example code will force the script function where it is used to be in-lined on compile.