GMS 2.3+ Binding Functions to Global Scope Undocumented Behavior?

samspade

Member
In doing some experimenting it seems like the following is valid, but according to the manual it is undocumented.

GML:
///Inside An Instance (not a script asset)

global.a_variable = 0;

a_function = method(global, function() {
    a_variable += 10;
    show_debug_message(a_variable);
});

a_function(); //prints the value 10
This makes sense, but is undocumented as the manual page for method says the following: method(struct_ref_or_instance_id, function); Unless of course global is an instance id, but if so you would expect it to work in a with statement (which it doesn't).

I'm not sure how much value understanding this has, but while I was working through how method variables and bindings work, I thought I would try this and it turns out to be valid.
 

Bart

WiseBart
I found out similar things during my own experiments.
There are some other "interesting" things.

Everything that you assign to a variable inside a script resource actually gets assigned to global.
Let's say you have the following script:
GML:
// Script assets have changed for v2.3.0 see
// https://help.yoyogames.com/hc/en-us/articles/360005277377 for more information
function Script1(){

}

a = "aaa";
b = "bbb";

set_x = function(val) {
    x = val;
}
You can then find out which names are identified globally using variable_struct_get_names:
GML:
show_debug_message(variable_struct_get_names(global));    // Is global a struct??
// [ "a","ObjectContainer","b","Script1","set_x" ]
log(global.a);
/// aaa
It works for some reason.

On the other hand:
GML:
show_debug_message(is_struct(global));
// 0

Not sure what's going on behind the scenes that makes this all work but it's interesting to say the least.
 

TheouAegis

Member
global has always been a struct, we just never knew what it actually was because the manual and tech notes never explicitly said it (and consequently they never made much sense). As for is_struct() failing, it's probably because global is a struct at the base level, not a user-created struct. You know, like how is_bool() is false for everything the manual says is a bool.
 

Joe Ellis

Member
So global is actually a built in global variable holding a struct id lol, I wonder what other things it can be used with that we couldn't do before

However it's undocumented so it might change at some point. I'd be careful what you use it with, or make sure there's an alternative you can resort to if it does change.
 
Last edited:

samspade

Member
Everything that you assign to a variable inside a script resource actually gets assigned to global.
This is in the manual, so I would consider it expected and reliable.

global has always been a struct, we just never knew what it actually was because the manual and tech notes never explicitly said it (and consequently they never made much sense). As for is_struct() failing, it's probably because global is a struct at the base level, not a user-created struct. You know, like how is_bool() is false for everything the manual says is a bool.
This is interesting. Since both is_struct and with fail with global, I wouldn't consider it a struct in the GMS 2.3 Struct since of the word, but it does seem like under-the-hood it is probably a struct or struct like thing. Since it isn't defined in the manual I'm happy leaving it with that and overall, I think that answers my question.

Ultimately, I don't think there's much point to binding a method to the global 'struct' either by creating a method variable in the script asset (which would bind it to the global level by default) or by explicitly using method to do so as it seems like the only thing that would achieve is removing the need to prefix variables with global inside of the function itself which I would consider at best a somewhat pointless semantic thing and at worst a bad thing as it obscures what is going on. But it has been useful to think about in understanding how method variables and binding works.
 
Top