• 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!

can't return a decoded json struct from a ( global / anonymous ) method, possible bug?

saffeine

Member
i'm just about headed to bed so i'm going to post this and check again at a later time but i'm having an issue when trying to return a decoded json struct from a method, and i need help figuring out if it's a bug or not.
i've narrowed it down to two main methods, so hopefully it's easily replicable if need be. the only thing i will note is that both methods are declared in a global struct ( global.InputHandler_ ), accessed through a macro ( InputHandler ), via a script.

"Script1":
GML:
#macro InputHandler        global.InputHandler_
global.InputHandler_    = {
    save    : function(){
        var _encoded    = {
            one: 1,
            two: 2.0,
            three: "three"
        };
      
        var _file    = file_text_open_write( game_save_id + "bindings.json" );
        file_text_write_string( _file, json_stringify( _encoded ) );
        file_text_close( _file );
      
        return json_stringify( _encoded );
    },
  
    load    : function(){
        var _file    = file_text_open_read( game_save_id + "bindings.json" );
        var _read    = file_text_read_string( _file );
        file_text_close( _file );

        var _decoded    = json_parse( _read );
        show_debug_message( _decoded ); // this prints the parsed struct to the console without an issue. 
        return _decoded; // the code that receives this value also tries to print to the console, but it's just blank.

        // also doesn't work: return json_parse( _read );
    }
};
and just for good measure, here's a screenshot of the console that shows what i mean. ( the result was converted to a string in both instances to add the prefixes ).
GameMakerStudio_jXNwHHv1rU.png

i can return a normal struct and i can print the decoded struct from within the method, so something tells me this is a bug. i just want to make sure before i open a ticket about it.
it's also possible that it could be due to the garbage collector removing the struct as it's passed back, but i personally don't know how to debug something like that.
can anyone confirm whether i'm doing something wrong or if i'm right in thinking that this isn't supposed to be the case? thanks.

-

UPDATE: moments after posting this i decided to see if it would work when calling from a struct within the instance itself ( code below ).
this worked completely fine, so it might be related to global structs, or i'm just a dummy. i'll be leaving the post as-is so i can get some sleep, but i'll poke around some more when i get the time.

GML:
/*
    i put this inside a create event.
    note: it still uses a bunch of project-exclusive things,
    but i'm hoping that it's useful enough as an example.
*/

test = {
    load    : function(){
        var _file    = file_text_open_read( game_save_id + "bindings.json" );
        var _read    = file_text_read_string( _file );
        file_text_close( _file );

        var _decoded    = json_parse( _read );
        show_debug_message( "within method: " + string( _decoded ) );
        return _decoded; // this seems to work completely fine now for some reason.
    }
}

show_debug_message( "outside method: " + string( test.load() ) );
GameMakerStudio_MQFRd2yIrO.png
 

Attachments

Last edited:
Top