static_get

该函数返回给定函数或结构体的 静态结构体

当你提供一个函数或方法时,该函数返回该函数或方法的静态结构体。

你也可以提供一个结构体。返回的内容取决于结构体:

注意:你可以将 method_get_index 的结果传递给该函数,以获取方法的静态结构体。

请参阅:静态结构

 

语法:

static_get(struct_or_func_name);

参数类型描述
struct_or_func_nameStructFunction or Method要获取静态结构体的结构体、函数或方法

 

返回:

Struct or undefined (for the root struct)

 

示例1:

function counter()
{
    static count = 0;
    return count ++;
}

repeat (10) counter()

// Get static struct of counter()
var _static_counter = static_get(counter);

// Both of these read the same variable
show_debug_message(counter.count); // 10
show_debug_message(_static_counter.count); // 10

上面的代码创建了带有静态变量的函数 counter()。该函数被重复调用,因此其静态变量的值会增加。

然后返回该函数的静态结构,并将其存储在变量 (_static_counter) 中。然后,它打印函数中的静态变量,方法是首先直接从函数中读取静态变量 (counter.count),然后从静态结构中读取它 (_static_counter.count)。两者都打印相同的值,因为它们引用完全相同的变量。

 

示例 2:在静态链中向上移动

function item() constructor
{
    static hello = function()
    {
        show_debug_message("Hello World!");
    }
}
function potion() : item() constructor {}

my_potion = new potion();
var _static_potion = static_get(my_potion);
var _static_parent = static_get(_static_potion);
_static_parent.hello();

上面的代码首先创建两个构造函数:带有单个静态函数 hello 的父构造函数 item 和子构造函数 potion。 然后,它创建一个新的 potion 并将其存储在变量 my_potion 中。 接下来,调用 static_get 以获取 my_potion 的静态结构。 返回的静态结构体存储在临时变量 _static_potion 中,是静态链的一部分。 从此时起,对 static_get 的所有进一步调用都将在静态链中向上移动。 再次调用 static_get,返回 item 的静态值并将其存储在另一个临时变量 _static_parent 中。 最后,调用该结构体的 hello 方法。