此函数从结构中给定的命名变量获取值。
您提供结构引用以及变量名称来获取字符串形式的值(请参阅下面的示例代码)。 该函数将返回变量所保存的值,如果指定的变量不存在,则返回undefined。
重要! 如果您获取的变量不存在,那么函数将返回关键字undefined,并且您可能会收到错误,这些错误将使游戏无法正常运行,因此,如果有疑问,请首先使用函数variable_struct_exists。
struct_get(struct, name);
参数 | 类型 | 描述 |
---|---|---|
struct | Struct | 要使用的结构引用 |
name | String | 要获取的变量的名称 (作为字符串) |
Any (any data type) or undefined (if the named variable does not exist)
if (struct_exists(mystruct, "shields"))
{
var ss = struct_get(mystruct, "shields");
}
else
{
var ss = -1;
}
上面的代码将检查一个变量是否存在于给定的结构中,如果它存在,那么它保存的值将被检索并存储在局部变量中。如果不存在,则将局部变量设置为-1。