gml_pragma

gml_pragma 函数会影响给定目标编译代码的方式,应使用不同的命令进行调用,以进一步优化项目的最终编译。这些命令会在游戏编译之前进行有效的 预处理 ,因此该函数可以放置在项目中的任何位置,并且在游戏完全编译之前仍然会被处理。可用的命令如下:

注意gml_pragma 函数的第一个参数 必须是编译时字符串常量 而不是变量。

编译器优化 编译器优化

编译器优化可以通过 "optimise"( 或 "optimize") 编译指示来提供。它们的基本语法是:

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

"<specific_optimisation>" 是包含要修改的特定优化的字符串。

"<control>" 是一个字符串,其中包含以逗号分隔的命令列表,这些命令从代码中的此时开始控制优化:

下表列出了您可以使用的所有优化:

Compiler Optimisations
Optimisation描述
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.

例子

例如,优化以下函数:

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

然后,您可以添加 gml_pragma 语句,如下所示:

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" );
}

使用说明

 

 

语法:

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

参数类型描述
commandString带有上面列出的命令之一的字符串。
[optional]String一些可用的命令需要一个或多个可选参数。上面为每个命令解释了这些。

 

返回:

N/A

 

例子:

gml_pragma("forceinline");

上面的示例代码将强制编译时使用内联的脚本函数。