r/gamemaker•
Gompomp

variable_instance_set not working?

Resolved

I'm trying to use variable_instance_set to declare and initialize a new variable, but when I check afterwards to see if the created variable exists, it does not.

Here's my code:

variable_instance_set(instance_id, "copper", 5);

if (variable_instance_exists(instance_id, "copper")) {
	
	var msg = "Exists!";
	
} else {
	
	var msg = "Does not Exist!";
	
} 

show_message(msg);

Posted Sep 13, 2020, 8:09 AM UTC

18
_TickleMeElmo_there is no tutorial for that•

instance_id is a build-in variable. It's an array and read-only and therefore does not contain a valid ID. Use a different variable name or access an ID in the array like the documentation describes.

1
GompompOP•

The documentation I read says the first slot is for the id of the instance that the variable belongs to.

1
_TickleMeElmo_there is no tutorial for that•

The "first slot"? The first parameter of the function you mean? Yeah - that's a bit stupid from the documentation to call it that.

Anyway, if instance_id is highlighted in your code like build in variables like x and y, I'd change it.

How to you set instance_id?

1
GompompOP•

If the first parameter isn't the instance id for the instance the variable belongs to, then what is it supposed to be?

1
_TickleMeElmo_there is no tutorial for that•

The first parameter is the instance, no doubt. You just can't call it the exact name instance_id used in the documentation because that is also the name of a build-in readonly variable.

That's why I asked how you set instance_id in your example...

1