• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code How to use variable_instance_get with alarms?

Slyddar

Member
Hey guys,

variable_instance_get(instance_id, name) takes an id and a string, and returns the id's variable value. When trying to use it with any alarms, e.g. "alarm[0]", it returns undefined, even though the alarm is set and active.

Anyone know how to use it like that? Could possibly be a bug, and I know the help file mentions the function is primarily used in compatibility scripts, but still it's a GMS2 function, and it should still perform as expected.
 

FrostyCat

Redemption Seeker
The brackets are not part of the variable name, that's why your attempt doesn't work.

The weird thing about alarm in particular is that though it is like an array in syntax, it is not an array at heart. It's the kind of behaviour that can also be observed with argument in scripts (source). For example, this would have been legal with any other instance-scoped array, but illegal with alarm:
Code:
// ILLEGAL
alarm[0] = 30;
var al = variable_instance_get(id, "alarm");
show_message(al[0]);
And the odd thing is that alarm[0] alone is accessible this way. Tested on runtime 2.2.3.344:
Code:
alarm[0] = 30;
show_message(variable_instance_get(id, "alarm")); //30
Code:
alarm[0] = 30;
var al = alarm;
show_message(al); //30
In any case, I would categorize this as undefined behaviour and not try to use it this way. If you genuinely want to access alarms by string reference, you should fake the alarm layer like this and access the fakes instead.

And as an update to recent practice, variable_instance_get() and its sister functions have seen a revival as a means of passing instance and global variables by reference, notably in libraries like GMTween. It has outgrown the label that the Manual ascribed to it quite some time ago.
 

Slyddar

Member
I appreciate you taking the time to write a detailed response @FrostyCat. Really helps to understand the problem, and is interesting to see some of the quirks.

I'm creating a room to room save system where each instance I want to save, simply populates their own ds_list of string variables they need to save, and I do the processing to take care of it at room_start/end, utilising variable_instance_get(). For that reason, I can't use the fake method. If any alarms are added, I'll just have to use a switch statement to grab the values directly instead of relying on the variable_instance_get function. More painful, but if GML doesn't provide a method, I'll make do.

Thanks again.
 
Top