variable_instance_get

使用此函数,您可以从给定的命名变量中获取值。您提供唯一的实例ID值 (可以从房间编辑器中的实例属性中找到该值,或者在调用函数instance_create_layer()以及变量的名称以获取的值作为字符串时返回 (请参见下面的示例代码)。函数将返回变量持有的值,如果变量不存在,则返回undefined

重要!如果您获取的变量不存在,则函数将返回关键字undefined,并且您可能会收到错误,这些错误将使游戏无法正常运行,因此,如果有疑问,请首先使用函数variable_instance_exists

 

语法:

variable_instance_get(instance_id, name);

参数类型描述
instance_idObject Instance要使用的实例的唯一ID值
nameString要获取的变量的名称 (作为字符串)

 

返回:

Any (any data type) or undefined (if the named variable does not exist)

 

例子:

if (variable_instance_exists(id, "shields"))
{
    var ss = variable_instance_get(id, "shields");
}
else
{
    var ss = -1;
}

上面的代码将检查是否存在变量,如果存在,则检索并存储在局部变量中。如果不存在,则将局部变量设置为-1。