SOLVED JSON_Stringify returns null

Beedub

Member
Hi,

I'm still having issues with JSON_Stringify. I have a function based on Shaun Spaldings video called "GameMaker studio 2 - Best Saving and Loading Tutorial (2.3.1+)". (It's an awesome video)

The function is pretty straight forward:
1. An array is created and then the function gets each entity with the parent of PSAveMe.
2. Each entity is pushed to an array, and then after all entities are added json_stringify is used and then the file is written via a buffer.

This was working perfectly, but all of a sudden it stopped working and the variable getting the stringify returns a "null", nothing more.

I've spent a few hours debugging and tracking each variable to see if anything is undefined. I've also compared the code I'm using to the code in the tutorial, it all looks the same.

I've also taken the code that is pushed to an array and tested stringify on this, that works fine. It also works if I convert the array to a string, but not if I just pass the array into the json_stringify function.

I am running Gamemaker Studio 2 IDE version 2.3.7.603 Steam, Current Runtime 2.3.7.474.

Here is the code below, hopefully someone will spot something I've missed (After staring at a screen things often get overlooked after I am driven absolutely bats!)

GML:
function saveLevel() {
   
    // Set local variables
    var _levData = array_create(0);
   
    // This is only here to show the same data in a string being converted to JSON
    var _teststringify = [ { x : 0, y : 252, image_index : 0.25, obj : "obj_ladder" } ];
    var _teststring = json_stringify(_teststringify);
    show_debug_message(_teststring);

// For every instance, create a struct and add it to the array
        with (pSaveMe)
        {
            var _saveEntity =
            {
            obj : object_get_name(object_index),
            y : y,
            x : x,
            image_index: image_index,
            }
           
            array_push(_levData,_saveEntity);
        }
   
    // turn the array into a JSON string and save it via a buffer
       
    var _string = json_stringify(_levData); // <--- This is where it all goes to pot
   
    var _buffer = buffer_create(string_byte_length(_string)+1, buffer_fixed, 1);
    buffer_write(_buffer, buffer_string, _string);
    buffer_save(_buffer, "levelsave.json");
    buffer_delete(_buffer);
   
}
Has anybody had this issue?
 
Last edited:

Beedub

Member
Now it's started working again, no changes were made to the code. Arrgh.

However... It only works the first time, if I save again in the same session the "NULL" returns and wipes out the data in the file.

It goes from this:

1638494611009.png

To this:

1638494519072.png
 
Last edited:
Have you tried saving it as a plain text file instead of a buffer? That might narrow it down to whether it's the json that's screwing up or the buffer itself.
 

Beedub

Member
I will give that a go, but if I pause the script (in debug mode) after the json_stringify line and before the buffer is created. the variable _string is null where it normally is holding the json string as text.
 
Ah, ok. Seems a bit strange, I can't see anything immediately wrong with the code. I'd try stepping through the code with the debugger as it adds the data with array_push(), keeping track of what _levData holds to find out exactly when it's getting corrupted. Might be something to do with one of the structs being pushed into it being corrupted in some way (as in, perhaps it's accidentally trying to push something that's not an instance and so doesn't have the data it's trying to add to the struct, for example).
 

Beedub

Member
no worries peardox, I will post my result if I manage to sort it out. Hopefully it might help you or other community members with the same bug.
 

rwkay

GameMaker Staff
GameMaker Dev.
There is a bug that we unintenionally introduced in 2.3.7 we have fixed it for the Beta that will release soon (hopefully today) we are looking to do a 2.3.7 hotfix next week and I am putting this fix forward for inclusion

Russell
 

peardox

Member
Thought I was going crazy

This...

GML:
// Default settings struct
var _settings = {
    is_fullscreen: true
}

try { // Just in case something wierd is going on
    show_debug_message("Stringifying " + string(_settings) + " - " + typeof(_settings));
    var _json = json_stringify(_settings);
    show_debug_message("Stringifyed as " + _json + " - " + typeof(_json));
} catch(_exception) {
        show_debug_message("Caught Error : " + _exception.longMessage);
}
Outputs this...


Stringifying { is_fullscreen : 1 } - struct
Stringifyed as null - string


Oh well, no settings for me this week then, don't wanna use a beta for production so hope the hotfix is soon

That's a pretty serious bug!
 

Beedub

Member
@rwkay Thats a relief, I thought I was going crazy. I spent hours looking at the code ripping my hair out. I'm bald now :)

Thanks to everybody else for contributing to the thread. I really appreciate this friendly and helpful community.
 

Beedub

Member
I've downloaded the latest update and it appears the bug has been resolved. I can save multiple times without it destroying my file and it looks like my load and save is running reliably.
 
Top