Help Saving a JSON

@UlisesFreitas I used what you posted in the other thread but it wont read when i close and reopen the game

I can’t help here I don’t even know if Load jSON returns a number or true/false. Tomorrow I’m going to make some tests and if I figure out I’ll share my project with you here.

1 Like

Does the file already exist? Then delete it, because the first events has the condition that the save file doesn’t exist. So if it already exists, it won’t save.

I delete it every time i test it. it saves but it never loads it after i reopen the game.

Thank you it means a lot to me.

How do you confirm it never loads? Have you run the debugger and checked what is in TempSave?

You’re copying text into TempSave, but checking if it’s number. Because setting a variable to “{name:\“Jimmy\”}” (as JSON would be), the integer value of this is 0. But it’s also 0 if the text variable isn’t defined.

You need to set TempSave = "" just before the read, and then do a string comparison to check whether or not it’s still an empty string (“”).

Ok ill try that. if it isn’t to much to ask could I see an example known of the examples ive seen have had that.

Do you use the asynch version for loading (if you double click on the load action, it’ll tell you)? If you are, switch to the other file load.

Also, I just noticed you can store the fail/success in a variable, and check that. You’re better off using that than checking the text variable value. Here I’m storing the result into the text variable LoadResult :

And here’s how to use it :

Ok so it says t is loading however the Variable is always set back to the start (0).

Which variable - TempSave, Menu or another one?

I both at the beginning of the scene TempSave changes the value of menu so they should be equal equal.

??!!

Why? You should be using the value of LoadResult (or whatever you named that variable) to set the value of menu.

TempSave and Menu will never be equal - TempSave is a JSON string and Menu is a number.


And, as I wrote earlier:

So it will be 0 in value if it’s successful or not.

I didnt know how to do it any other way because you cant save a global variable.

No, I mean you should be using the LoadResult to set the global variable Menu to 0 or 1. like this :

After this, TempSave will be a structure of the loaded data (or variable if it’s just one value), and your global variable Menu will be set.

I see but Menu goes all the way to 18 for unlocking levels. so I wanted to save TempSave and make it equal and at the start of the game make TempSave equal to global. I now understand it wont read correctly and will always be 0. I really appreciate your patients and help, im sorry but I’m having a tough time implementing this correctly

No worries, we’ll get it working. What does TempSave look like as a variable when you save it - what value does it hold? And what are the contents of the save file?

1 Like

Ok, So I actually believe i got it to work. ill post my findings here after I double check 100 more times. hopefully it helps someone in the future. your help means more than you will ever know. Thank you, I’ve spent several days working till 3 A.M. and this feels good. hopefully I don’t have to ask for more help.

Actually, one last question how would i overwrite the previous save so it replaces it? when I hit the save button again

Just save the file to the same place. So no need to check if it exists, just save it, and it’ll overwrite what was there previously.

Also, if TempSave is a number, and the variable Menu is to take on the value of TempSave, you can do it in one line - just use the action “Set the value of Global Variable Menu = Variable(TempSave)”. No need to have an event to check for each value of TempSave and then set the global variable.

1 Like