gml_pragma 函数会影响给定目标编译代码的方式,应使用不同的命令进行调用,以进一步优化项目的最终编译。这些命令会在游戏编译之前进行有效的 预处理 ,因此该函数可以放置在项目中的任何位置,并且在游戏完全编译之前仍然会被处理。可用的命令如下:
gml_pragma("global", "Init()");
This will call the script function "Init" before the first room of the game is run. Note that the GML supplied as the second argument must be a compile-time constant, and also note that you cannot use this pragma to create instances or perform any operations that require a room (or anything in a room) to function.gml_pragma("optimise", "js_array_check", "push, off");
This pragma will make the compiler omit type checking code on arrays when it generates JavaScript code.gml_pragma("Texgroup.Scale", "level1", "2");
This will halve all the textures in the "level1" texture group.gml_pragma("UnityBuild", "true");
The benefit of doing a unity build is that builds are faster but the down side is that it does a full build each time so even if you change a single part of the code it will build everything again without using any cached files. This has been added specifically for the Xbox One export using the YYC although it can be called for other builds (YYC only). For more information on unity builds, please see here.注意gml_pragma 函数的第一个参数 必须是编译时字符串常量 而不是变量。
编译器优化可以通过 "optimise"( 或 "optimize") 编译指示来提供。它们的基本语法是:
gml_pragma("optimise", "<specific_optimisation>", "<control>");
"<specific_optimisation>" 是包含要修改的特定优化的字符串。
"<control>" 是一个字符串,其中包含以逗号分隔的命令列表,这些命令从代码中的此时开始控制优化:
下表列出了您可以使用的所有优化:
Optimisation | 描述 |
---|---|
js_array_check | If 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_check | If 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_index | If 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_long | If 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_ops | If 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...]);
参数 | 类型 | 描述 |
---|---|---|
command | String | 带有上面列出的命令之一的字符串。 |
[optional] | String | 一些可用的命令需要一个或多个可选参数。上面为每个命令解释了这些。 |
N/A
gml_pragma("forceinline");
上面的示例代码将强制编译时使用内联的脚本函数。