Question - Code How to use variable_instance_get with alarms?

Discussion in 'GameMaker Studio 2 Community Tech Support' started by TheSly, Aug 16, 2019.

  1. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    977
    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.
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,450
    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.
     
  3. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    977
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice