GMS 2.3+ Binding Function to a Destroyed Instance, why does this work?

samspade

Member
I'm still working through the various new features of 2.3 and I've come across another question:

GML:
///object 1 create event

my_variable = 100;
global.global_func = function() {
    my_variable += 10;
    show_debug_message(my_variable);
    show_debug_message("Hello?");
}

instance_destroy();
GML:
///object 2 some event that fires after object 1's create event

global.global_func();
Not only does the above code not error out, it will print 110 to the output line despite the fact object 1 is destroyed (which can be verified in the debugger and through other methods such as trying to reference the instance id).

How, or perhaps why, does this work? The why sort of makes sense, as the function is bound to an instance of object 1 and the reference to that function hasn't been lost because it is stored in a global variable, but on the other hand the instance is destroyed and the variable, my_variable presumably is as well, except that it isn't and can still be referenced through the use of this function.
 

Joe Ellis

Member
It might be the garbage collector, the same way that an array will still exist if something has a reference to it. Maybe they've changed the engine to handle destroyed instances in the same way. If that's the case I think it's good, cus you might want to reference what a variable was of an instance after it's been destroyed. It would actually make a lot of handy possibilities.

Also once nothing has a reference to it anymore and it actually gets deleted, there's not really any case or possibility of something trying to get the id reference of it anymore, so no errors would be possible. I actually really hope that this is the case. 2.3 is a gift that keeps on giving! 🤗
 

samspade

Member
If that's the case I think it's good, cus you might want to reference what a variable was of an instance after it's been destroyed. It would actually make a lot of handy possibilities.
This doesn't work though. There's no way to reference an instance without using the function (e.g. instance doesn't work and you can return the id in the function and try to use it and it won't work either). My guess is that the first part is right, because there is a reference to it, it sticks around somewhere but all the ways GameMaker gives you to reference it (e.g. instance id which might in theory be looking things up in a list or something) no longer work or are connected.
 

Joe Ellis

Member
That's a shame, this functionality could be really useful, we should put in a suggestion to yoyo, I'm sure it wouldn't be that hard for them to let this work, kind of not doing certain things instead of having to build some complex thing to handle it. Yeah I'd like to be able to do:
if destroyed_instance.variable = value, or variable = destroyed_instance.variable.

It would also help beginners on a massive scale with all the problems related to the order that instance's step events are executed.
 

FrostyCat

Member
This is likely a side effect of the change to self. Instead of binding to an instance ID, internally the function is bound to an underlying struct (which you can see using self). So even if the instance ID now points nowhere, the struct still exists through the reference in the bound method.
 
Top