variable_instance_set

With this function you can set the value of a given variable in an instance. You supply the unique instance ID value (which can be found from the Instance Properties in the room editor, or is returned when you call the function instance_create_layer()) as well as the name of the variable to set the value of as a string (see example code below), and then finally the value to set (can be any valid data type). If the variable does not exist already in the instance it will be created and then assigned the value.

 

Syntax:

variable_instance_set(instance_id, name, val);

ArgumentTypeDescription
instance_idObject InstanceThe unique ID value of the instance to use
nameStringThe name of the variable to set (as a string)
valAnyThe value to set the variable to

 

Returns:

N/A

 

Example:

if (!variable_instance_exists(id, "shields"))
{
    variable_instance_set(id, "shields", 0);
}

The above code will check to see if an instance variable exists in the calling instance and if it does not then it is created and set to 0.