[SOLVED]Save procedure

Hi,

I'm writing a set of save/load routines and I wanted to know if this code looks correct?

Code:
var sentry_count = instance_number(SentryBatObject);

if (file_exists("player_save.sav"))
{
    file_delete("player_save.sav");
    
}

var saveFile = file_text_open_write("player_save.sav");


for (var i = 0; i< sentry_count;i++)
{

        var instance = instance_find(Player, 0);
        
        //convert instance to a ds_map
        var instanceMap = ds_map_create();

        ds_map_add(instanceMap, "global-hasbat1", global.hasbat1);
        ds_map_add(instanceMap, "global-hasbatA", global.hasbatA);
        ds_map_add(instanceMap, "global-hasbatB", global.hasbatB);
        ds_map_add(instanceMap, "global-hasbatC", global.hasbatC);
        ds_map_add(instanceMap, "global-hasbatD", global.hasbatD);
        ds_map_add(instanceMap, "global-hasbatE", global.hasbatE);
        ds_map_add(instanceMap, "global-hasbatF", global.hasbatF);
        ds_map_add(instanceMap, "global-hasbatG", global.hasbatG);
        ds_map_add(instanceMap, "global-hasbatH", global.hasbatH);




        ds_map_add(instanceMap, "global-current_bat", global.current_bat);
        ds_map_add(instanceMap, "global-select_bbat", global.select_bat);
        ds_map_add(instanceMap, "global-dropped_bat", global.dropped_bat);


        ds_map_add(instanceMap, "global-windbat", global.windbat);
        ds_map_add(instanceMap, "global-poisonbat", global.poisonbat);
        ds_map_add(instanceMap, "global-snowbat", global.snowbat);
        ds_map_add(instanceMap, "global-musclebat", global.musclebat);
        ds_map_add(instanceMap, "global-bombbat", global.bombbat);
        ds_map_add(instanceMap, "global-magicbat", global.magicbat);
        



        ds_map_add(instanceMap, "global-timerbatA", global.timerbatA);
        ds_map_add(instanceMap, "global-timerbatB", global.timerbatB);
        ds_map_add(instanceMap, "global-timerbatC", global.timerbatC);
        ds_map_add(instanceMap, "global-timerbatD", global.timerbatD);
        ds_map_add(instanceMap, "global-timerbatE", global.timerbatE);
        ds_map_add(instanceMap, "global-timerbatF", global.timerbatF);
        ds_map_add(instanceMap, "global-timerbatG", global.timerbatG);
        ds_map_add(instanceMap, "global-timerbatH", global.timerbatH);

        ds_map_add(instanceMap, "global-bhourglass", global.bhourglass);
        ds_map_add(instanceMap, "global-chourglass", global.chourglass);
        ds_map_add(instanceMap, "global-dhourglass", global.dhourglass);
        ds_map_add(instanceMap, "global-ehourglass", global.ehourglass);
        ds_map_add(instanceMap, "global-fhourglass", global.fhourglass);
        ds_map_add(instanceMap, "global-ghourglass", global.ghourglass);
        ds_map_add(instanceMap, "global-hhourglass", global.hhourglass);
        
//alchemist hourglass

        ds_map_add(instanceMap, "global-alch_hourglass", global.alch_hourglass);

        ds_map_add(instanceMap, "global-batBtimer", global.batBtimer);
        ds_map_add(instanceMap, "global-batBcountdown", global.batBcountdown);
        ds_map_add(instanceMap, "global-batCtimer", global.batCtimer);
        ds_map_add(instanceMap, "global-batCcountdown", global.batCcountdown);
        ds_map_add(instanceMap, "global-batDtimer", global.batDtimer);
        ds_map_add(instanceMap, "global-batDcountdown", global.batDcountdown);
        ds_map_add(instanceMap, "global-batEtimer", global.batEtimer);
        ds_map_add(instanceMap, "global-batEcountdown", global.batEcountdown);
        ds_map_add(instanceMap, "global-batFtimer", global.batFtimer);
        ds_map_add(instanceMap, "global-batFcountdown", global.batFcountdown);
        ds_map_add(instanceMap, "global-batGtimer", global.batGtimer);
        ds_map_add(instanceMap, "global-batGcountdown", global.batGcountdown);
        ds_map_add(instanceMap, "global-batHtimer", global.batHtimer);
        ds_map_add(instanceMap, "global-batHcountdown", global.batHcountdown);









        ds_map_add(instanceMap, "global-active", global.active);

        ds_map_add(instanceMap, "global-select_bat", global.select_bat);

        ds_map_add(instanceMap, "attacking_castle", instance.attacking_castle);

        ds_map_add(instanceMap, "global-castle_hp", global.castle_hp);
        ds_map_add(instanceMap, "global-wall_hp", global.wall_hp);
        ds_map_add(instanceMap, "global-loading_flag", global.loading_flag);


        ds_map_add(instanceMap, "global-is_bomb_bat", global.is_bomb_bat);

….


        ds_map_add(instanceMap, "poison_battle_timer", instance.poison_battle_timer);
        
//////////////RETREAT/////////////

        ds_map_add(instanceMap, "flight", instance.poison_flightbattle_timer);
        ds_map_add(instanceMap, "pplay_timer", instance.play_timer);
        ds_map_add(instanceMap, "played", instance.played);
        ds_map_add(instanceMap, "inst", instance.inst);

////////////////////difficulties////////////

        ds_map_add(instanceMap, "global-easy_difficulty", global.easy_difficulty);
        ds_map_add(instanceMap, "global-medium_difficulty", global.medium_difficulty);
        ds_map_add(instanceMap, "global-hard_difficulty", global.hard_difficulty);
        ds_map_add(instanceMap, "global-doom_difficulty", global.doom_difficulty);

        
        //convert this map into json
        JSONInstance = json_encode(instanceMap);
        file_text_write_string(saveFile, JSONInstance);
        //seperate with pipe character
        
        
        ds_map_destroy(instanceMap);       
        
        
}

///close the file
file_text_close(saveFile);
Also where does the text write routine write to? %localappdata% ?
I checked there and couldn't find my sav files
 

Relic

Member
Looks ok, except you do a lot of repeating code. The only thing that looks like it changes between instances is this section:

Code:
        ds_map_add(instanceMap, "flight", instance.poison_flightbattle_timer);
        ds_map_add(instanceMap, "pplay_timer", instance.play_timer);
        ds_map_add(instanceMap, "played", instance.played);
        ds_map_add(instanceMap, "inst", instance.inst);
Everything else being written to the map is global scoped - not a huge deal but you wouldn't need to save these details in every single instanceMap.

It also looks like you are trying to put multiple JSONs in a single file - using file_text_write_string will remove all previous data in the text file (according to the manual) - so it will only store the last sting you add (I think! Not done this myself).
 

chamaeleon

Member
I find it fascinating that the code shown has a huge amount of entries to write, none of which use the for loop variable.
 
@chamaeleon I find it fascinating that you think that I don't know that those can be saved in one instance. I haven't gotten to that point yet. Everyone thinks differently and approaches
problems differently, that includes both you and me.

It also looks like you are trying to put multiple JSONs in a single file - using file_text_write_string will remove all previous data in the text file (according to the manual) - so it will only store the last sting you add (I think! Not done this myself).
What exactly do you mean? Could you elaborate? Also, I'm running the save script and its not writing to a file?
 

Relic

Member
Since file_text_write_json is called every iteration of the loop, it will keep overwriting the contents in the text file - as described in the manual.

I don’t know why your text file is not being created at all.
 

chamaeleon

Member
@chamaeleon I find it fascinating that you think that I don't know that those can be saved in one instance. I haven't gotten to that point yet. Everyone thinks differently and approaches
problems differently, that includes both you and me.
That is certainly true. Anyway, have you looked in the folder represented by working_directory for your file? Is instance_number(SentryBatObject) returning a number greater than zero so the loop content is executed?
 
That is certainly true. Anyway, have you looked in the folder represented by working_directory for your file? Is instance_number(SentryBatObject) returning a number greater than zero so the loop content is executed?
I looked in the working_directory, there is nothing there. About the repeated overwriting of the save files, would this fix it?
Code:
var sentry_count = instance_number(SentryBatObject);

if (file_exists("player_save.sav"))
{
    file_delete("player_save.sav");
    
}

var saveFile = file_text_open_write("player_save.sav");



for (var i = 0; i< sentry_count;i++)
{

        var instance = instance_find(Player, 0);
        
        //convert instance to a ds_map
        var instanceMap = ds_map_create();

        ds_map_add(instanceMap, "global-hasbat1", global.hasbat1);
        ds_map_add(instanceMap, "global-hasbatA", global.hasbatA);
        ds_map_add(instanceMap, "global-hasbatB", global.hasbatB);
        ds_map_add(instanceMap, "global-hasbatC", global.hasbatC);
        ds_map_add(instanceMap, "global-hasbatD", global.hasbatD);
        ds_map_add(instanceMap, "global-hasbatE", global.hasbatE);
        ds_map_add(instanceMap, "global-hasbatF", global.hasbatF);
        ds_map_add(instanceMap, "global-hasbatG", global.hasbatG);
        ds_map_add(instanceMap, "global-hasbatH", global.hasbatH);




        ds_map_add(instanceMap, "global-current_bat", global.current_bat);
        ds_map_add(instanceMap, "global-select_bbat", global.select_bat);
        ds_map_add(instanceMap, "global-dropped_bat", global.dropped_bat);


        ds_map_add(instanceMap, "global-windbat", global.windbat);
        ds_map_add(instanceMap, "global-poisonbat", global.poisonbat);
        ds_map_add(instanceMap, "global-snowbat", global.snowbat);
        ds_map_add(instanceMap, "global-musclebat", global.musclebat);
        ds_map_add(instanceMap, "global-bombbat", global.bombbat);
        ds_map_add(instanceMap, "global-magicbat", global.magicbat);
        



        ds_map_add(instanceMap, "global-timerbatA", global.timerbatA);
        ds_map_add(instanceMap, "global-timerbatB", global.timerbatB);
        ds_map_add(instanceMap, "global-timerbatC", global.timerbatC);
        ds_map_add(instanceMap, "global-timerbatD", global.timerbatD);
        ds_map_add(instanceMap, "global-timerbatE", global.timerbatE);
        ds_map_add(instanceMap, "global-timerbatF", global.timerbatF);
        ds_map_add(instanceMap, "global-timerbatG", global.timerbatG);
        ds_map_add(instanceMap, "global-timerbatH", global.timerbatH);

        ds_map_add(instanceMap, "global-bhourglass", global.bhourglass);
        ds_map_add(instanceMap, "global-chourglass", global.chourglass);
        ds_map_add(instanceMap, "global-dhourglass", global.dhourglass);
        ds_map_add(instanceMap, "global-ehourglass", global.ehourglass);
        ds_map_add(instanceMap, "global-fhourglass", global.fhourglass);
        ds_map_add(instanceMap, "global-ghourglass", global.ghourglass);
        ds_map_add(instanceMap, "global-hhourglass", global.hhourglass);
        
//alchemist hourglass

        ds_map_add(instanceMap, "global-alch_hourglass", global.alch_hourglass);

        ds_map_add(instanceMap, "global-batBtimer", global.batBtimer);
        ds_map_add(instanceMap, "global-batBcountdown", global.batBcountdown);
        ds_map_add(instanceMap, "global-batCtimer", global.batCtimer);
        ds_map_add(instanceMap, "global-batCcountdown", global.batCcountdown);
        ds_map_add(instanceMap, "global-batDtimer", global.batDtimer);
        ds_map_add(instanceMap, "global-batDcountdown", global.batDcountdown);
        ds_map_add(instanceMap, "global-batEtimer", global.batEtimer);
        ds_map_add(instanceMap, "global-batEcountdown", global.batEcountdown);
        ds_map_add(instanceMap, "global-batFtimer", global.batFtimer);
        ds_map_add(instanceMap, "global-batFcountdown", global.batFcountdown);
        ds_map_add(instanceMap, "global-batGtimer", global.batGtimer);
        ds_map_add(instanceMap, "global-batGcountdown", global.batGcountdown);
        ds_map_add(instanceMap, "global-batHtimer", global.batHtimer);
        ds_map_add(instanceMap, "global-batHcountdown", global.batHcountdown);









        ds_map_add(instanceMap, "global-active", global.active);

        ds_map_add(instanceMap, "global-select_bat", global.select_bat);

        ds_map_add(instanceMap, "attacking_castle", instance.attacking_castle);

        ds_map_add(instanceMap, "global-castle_hp", global.castle_hp);
        ds_map_add(instanceMap, "global-wall_hp", global.wall_hp);
        ds_map_add(instanceMap, "global-loading_flag", global.loading_flag);


        ds_map_add(instanceMap, "global-is_bomb_bat", global.is_bomb_bat);

….


        ds_map_add(instanceMap, "poison_battle_timer", instance.poison_battle_timer);
        
//////////////RETREAT/////////////

        ds_map_add(instanceMap, "flight", instance.poison_flightbattle_timer);
        ds_map_add(instanceMap, "pplay_timer", instance.play_timer);
        ds_map_add(instanceMap, "played", instance.played);
        ds_map_add(instanceMap, "inst", instance.inst);

////////////////////difficulties////////////

        ds_map_add(instanceMap, "global-easy_difficulty", global.easy_difficulty);
        ds_map_add(instanceMap, "global-medium_difficulty", global.medium_difficulty);
        ds_map_add(instanceMap, "global-hard_difficulty", global.hard_difficulty);
        ds_map_add(instanceMap, "global-doom_difficulty", global.doom_difficulty);

        
        convert this map into json
        
    add a pipecharacter to instanceMap to seperate between instances

continue loop

end loop
        
        write jsonstance to saveFile
    destroy instance map
 

chamaeleon

Member
I looked in the working_directory, there is nothing there. About the repeated overwriting of the save files, would this fix it?
Have a look in %LOCALAPPDATA%\<name of your program> instead of working_directory... And the manual is misleading about the overwriting part. Every write to it won't overwrite the previous write. What I think the manual wanted to express was that if you did not use an append open you'd overwrite. The text stating that writing starts at the beginning belongs more with the open function than the various write functions who should not need to know anything about the write mode. I believe your original code is producing the output as you expect it.
 
I looked at that directory and there was nothing. think I need to use append but still, there should be something showing up in that directory. What would cause it not to write to the disk? It couldn't have to do with me using write instead of append could it?
 
Last edited by a moderator:

SeraphSword

Member
I don't fully understand what you're trying to do. You're using a for loop based on the number of SentryBatObject instances, but the only instance you actually use is Player. Is Player the parent object for all of them? If so, your instance_find is only ever looking at the one at 0 and isn't iterating with the loop. Based on that it doesn't look like you need a loop at all, since everything is global except for the couple values that involve instance, and you're only looking at one instance. It seems like either you're missing something to iterate which id you are looking at to save, or mixed up which code goes where. Assuming that Player and SentryBatObject are connected in some way.

It's also possible I missed something, since I don't really use this method for saving and I'm hardly an expert.

Assuming I'm wrong on my first point and that your code is otherwise valid, I think it would make things more efficient to move every global scope variable above the for loop, use the for loop only for the values that involve instance, and move the encode part below the for loop, outside the brackets. It looks to me like you want to write all of these to one map, and then just encode it all into one file.
 
var alchemist_skull_counter = instance_number(SkullObject);
if (file_exists("skull.sav"))
{
file_delete("skull.sav");

}
var saveFile = file_text_open_write("skull.sav");

for (var i = 0; i< alchemist_skull_counter;i++)
{
var instance = instance_find(SkullObject, i);

//convert instance to a ds_map
var instanceMap = ds_map_create();

ds_map_add(instanceMap, "mouse_enter", instance.mouse_enter);
ds_map_add(instanceMap, "global-nonusable", global.non_usable);
ds_map_add(instanceMap, "global-gems", global.gems);
ds_map_add(instanceMap, "global-a_choose", global.a_choose);
ds_map_add(instanceMap, "skull_unlocked", instance.skull_unlocked);
ds_map_add(instanceMap, "cost_in_gems", instance.cost_in_gems);
ds_map_add(instanceMap, "timer", instance.timer);
ds_map_add(instanceMap, "active", instance.active);






JSONInstance = json_encode(instanceMap);

file_text_write_string(saveFile, JSONInstance);
//seperate with pipe character
file_text_write_string(saveFile, "|");

ds_map_destroy(instanceMap);



}
///close the file
file_text_close(saveFile);
 

Pfap

Member
That code looks good to me after glancing over it, maybe try writing out of the sand box? Write the data to a fie on your desktop and then open that to confirm it is working? I'm not sure where save files for a game run from the ide are kept. Also, are you using JSON for a specific reason? You could just supply a file and use ds_map_save().

This is probably a separate issue, but I was writing a buffer to a text file and noting was showing up, but I could still read from it...
Have you tried loading the data from the same code section? Like as soon as you close the file open it and read the data you just wrote.

Edit:
@Ted Gress
I played with your code and got it to work on my system so it is fine. Also, I meant to say ds_map_write();

This is the code I used if you want to try it:
Code:
var i, file, fileM;
i = 0;

fileM = get_save_filename("textfile|*.txt", "");
if fileM != ""{
 file = file_text_open_write(fileM); 
 
 
var alchemist_skull_counter = 5;

for (var i = 0; i< alchemist_skull_counter;i++)
{
var instance = 20;
//convert instance to a ds_map
var instanceMap = ds_map_create();
ds_map_add(instanceMap, "mouse_enter", "data");
ds_map_add(instanceMap, "global-nonusable", "data");
ds_map_add(instanceMap, "global-gems", "data");
ds_map_add(instanceMap, "global-a_choose", "data");
ds_map_add(instanceMap, "skull_unlocked", "data");
ds_map_add(instanceMap, "cost_in_gems", "data");
ds_map_add(instanceMap, "timer", "data");
ds_map_add(instanceMap, "active", "data");
JSONInstance = json_encode(instanceMap);
file_text_write_string(file, JSONInstance);
//seperate with pipe character
file_text_write_string(file, "|");
ds_map_destroy(instanceMap);

}
   file_text_close(file);
}
It's good to run, just create an empty text file on your desktop or anywhere and select it.
 
Last edited:

chamaeleon

Member
Ok. I'm completely lost as to why this isn't writting to disk and how to figure out the problem.
What happens if you try something a new project, with two objects (with sprites so you can mouse click on them) each with their own Mouse Left Released event?
Saver object Mouse Left Released
Code:
if (file_exists("json.sav")) {
    file_delete("json.sav");
}

var saveFile = file_text_open_write("json.sav");
var map = ds_map_create();
ds_map_add(map, "foo", 123);
ds_map_add(map, "bar", 456);
var json = json_encode(map);
file_text_write_string(saveFile, json);
ds_map_destroy(map);
file_text_close(saveFile);
Loader object Mouse Left Released
Code:
var saveFile = file_text_open_read("json.sav");
if (saveFile != -1) {
    var json = file_text_readln(saveFile);
    var map = json_decode(json);
    show_debug_message(json_encode(map));
    file_text_close(saveFile);
} else {
    show_debug_message("Couldn't open json.sav for reading");
}
Or make one object using left and right mouse buttons, or whatever you feel like, as long as you get the ability to invoke the two different code snippets separately.

I get the output I expect.
Code:
{ "bar": 456.000000, "foo": 123.000000 }
I would be very curious to find out if you do too.
 
Check the manual under File Handling->The File System, it will have a list of default save locations.

If you used the get_save_filename() function then it should be where ever you chose to put it.
 

Pfap

Member
I opened the file and it is blank.As in, I try to move the cursor and it doesn't move and there is no text and its a
text file.
I don't know how or where to open files that are saved from hitting the run button. I know they are in %LOCALAPPDATA%\ I don't know where to access that, I thought those files were hidden as there is really no need to ever access them. I have a feeling you are looking in the wrong spot and I don't know where the right spot is, but why don't you try to load from within a run of your project if you haven't? Otherwise, if you need an easily reachable human readable file why not explicitly give the path and use get_save_filename("textfile|*.txt", "")?
 
I don't know how or where to open files that are saved from hitting the run button. I know they are in %LOCALAPPDATA%\ I don't know where to access that, I thought those files were hidden as there is really no need to ever access them. I have a feeling you are looking in the wrong spot and I don't know where the right spot is
The files are written to working directory which is different dependent upon the device and the sandbox. Workingdirectory for windows builds is %localappdata%\projectname. I went to that directory and there was a file there, its just the file was empty. So gamemaker created a text file but there was no content written to the fire.

Code:
but why don't you try to load from within a run of your project if you haven't?
No point in trying to load an empty file.

Code:
Otherwise, if you need an easily reachable human readable file why not explicitly give the path and use get_save_filename("textfile|*.txt", "")?
I don't know what that is.
 
T

Taddio

Guest
Have you tried writing to an .ini instead?
From the look of it, it should save your stuff, I dunno...
If it works, you could then use ds_map_secure_save(), and it will still be encrypted.
@Pfap They're definately not hidden, and overall it's useful to be able to access them, if it's just to see if they're not obviously "hackable" in a game-breaking way.
 

Pfap

Member
Have you tried writing to an .ini instead?
From the look of it, it should save your stuff, I dunno...
If it works, you could then use ds_map_secure_save(), and it will still be encrypted.
@Pfap They're definately not hidden, and overall it's useful to be able to access them, if it's just to see if they're not obviously "hackable" in a game-breaking way.
Hmmm, I read something on some Microsoft forum... and I couldn't find them on my system. Could you point me in the right direction?
 
T

Taddio

Guest
Just realized it was indeed an "invisible" folder, I just have them set to visible since forever, so didn't noticed.

For me, they're in:
C:/user/appdata/local/game_projec_foldert_name
 
Come on guys. I'm getting really desperate now. It's been since March 12th and I'm stuck on the same problem and not making any progress. There has to be a reason that its not writing to the file. Could it be the JSON code?
 
Do a show_debug_message() on your JSON string and check it's correct and not blank.

Use the debugger to step through your code and check all the variables are what you expect.

From all the posts above, you have the code that you need. Now you just need to check the data I believe.

Probably because you are using file_text_write_string only, you are over writing your file in the loop. Check the last iteration of your loop in particular, that it's not writing a blank data to the file.

If you want these strings in the same file, you need to put a file_text_writeln () function between strings.
 

chamaeleon

Member
Probably because you are using file_text_write_string only, you are over writing your file in the loop. Check the last iteration of your loop in particular, that it's not writing a blank data to the file.
I don't believe this to be correct. I think the manual is misleading. It's the file_text_open_write() that erases everything and starts with a blank file. file_text_open_append() would be used to append to an existing file. Using file_text_write_string() multiple times, in a loop or not, would not have any bearing on it.
Code:
var data = ds_map_create();

data[? "foo"] = 1;
data[? "bar"] = 2;

var json = json_encode(data);

var f = file_text_open_write("test.txt");
file_text_write_string(f, json);
file_text_write_string(f, json);
file_text_close(f);

f = file_text_open_read("test.txt");
var read = file_text_read_string(f);
file_text_close(f);

show_debug_message(read);

ds_map_destroy(data);
Code:
{ "bar": 2.000000, "foo": 1.000000 }{ "bar": 2.000000, "foo": 1.000000 }
 
Thanks so much guys :) I got it to write disk and save but ran into another problem. When it saves the game completely freezes.
It still writes to disk and there is content but the game just freezes.
 
Wow this thread is so long it could be a wiki. Anyway app still freezing. I did notice that the files were written to the disk, so does that mean the problem is post-write?
 
T

Taddio

Guest
Freeze like in...?
Came crashes and closes? game doesn't close but doesn't do anything either? Do you have an error measage? What does the debugger says?
By now, you should expect that we need this kind of information, otherwise it's pretty much useless asking.
 
@Taddio Yeah your right I should have you given more information. So, I click on the alchemist table which triggers the save code. The game doesn't close but doesn't do anything either. It literally freezes. I can't click on anything and nothing animates. I don't get any error message, not even in the debugger. The debugger profiler stops updating as well.
upload_2019-3-27_21-39-40.png
 
T

Taddio

Guest
Unclick average values in the profiler, so we can see what happens every step.
You have a breakpoint in there, are you sure it's not just that that makes your game stop?
 
T

Taddio

Guest
Hard to tell...game looks like it's running (tho paused in the 2nd screenshot) and I don't see what could be causing this...I think you'll have to check what happens line by line and see what is really happening under the hood at that exact time
 
I traced the freeze to this code. Is this code correct? The line is

Code:
JSONInstance2 = json_encode(list1);

Code:
var brain_master_count = instance_number(BrainMaster);

if (file_exists("brain_master.sav"))
{
    file_delete("brain_master.sav");
    
}

var saveFile = file_text_open_write("brain_master.sav");



for (var i = 0; i< brain_master_count;i++)
{
        var instance = instance_find(BrainMaster, i);
        
        //convert instance to a ds_map
        var instanceMap = ds_map_create();
        var list1 =  ds_list_create();
        //
            
        ds_map_add(instanceMap, "timer", instance.timer);
        ds_map_add(instanceMap, "global-max_number_of_brains", global.max_number_of_brains);   
        ds_map_add(instanceMap, "global-number_of_brains_on_screen", global.number_of_brains_on_screen);
        ds_map_add(instanceMap, "global-creating_brain", global.creating_brain);
        
        ds_map_add(instanceMap, "global-frank_timer", global.frank_timer);
        ds_map_add(instanceMap, "global-delay_between_brains", global.delay_between_brains);
        ds_map_add(instanceMap, "frank_audio_timer", instance.frank_audio_timer);
        ds_map_add(instanceMap, "timer", instance.sprite_index);
        ds_map_add(instanceMap, "door", instance.door);
        ds_map_add(instanceMap, "door_timer", instance.door_timer);
        ds_map_add(instanceMap, "sprite_index", instance.sprite_index);
        
        ds_map_add(instanceMap, "game_win_flag", instance.game_win_flag);
        ds_map_add(instanceMap, "f_timer", instance.f_timer);
        ds_map_add(instanceMap, "g_timer", instance.g_timer);
        ds_map_add(instanceMap, "f_flag", instance.f_flag);
        ds_map_add(instanceMap, "global-current_level", global.current_level);

        ds_map_add(instanceMap, "global-you_lose", global.you_lose);
        ds_map_add(instanceMap, "random_y", instance.random_y);


        ds_map_add(instanceMap, "frank_audio_timer", instance.frank_audio_timer);
        ds_map_add(instanceMap, "f_timer", instance.f_timer);
        ds_map_add(instanceMap, "g_timer", instance.g_timer);
    


        ds_list_add(list1, object_get_name(FrankensteinIdleObject), FrankensteinIdleObject.object_index);
        //ds_list_add(list1, object_get_name(CastleDoorObject), CastleDoorObject.object_index);
        //ds_list_add(list1, object_get_name(LaserBrainObject), LaserBrainObject.object_index);



        //convert this map into json
        JSONInstance = json_encode(instanceMap);
        JSONInstance2 = json_encode(list1);
        file_text_write_string(saveFile, JSONInstance);
        file_text_write_string(saveFile, JSONInstance2);
        //seperate with pipe character
        file_text_write_string(saveFile, "|");
        
        ds_map_destroy(instanceMap);       
        ds_list_destroy(list1);



}

///close the file
file_text_close(saveFile);


///////
So that's where it freezes.
 
Ok. I changed some stuff around and now it freezes at
Code:
JSONInstance = json_encode(instanceMap)
This is the newly worked code:


Code:
var brain_master_count = instance_number(BrainMaster);

if (file_exists("brain_master.sav"))
{
    file_delete("brain_master.sav");
    
}

var saveFile = file_text_open_write("brain_master.sav");



for (var i = 0; i< brain_master_count;i++)
{
        var instance = instance_find(BrainMaster, i);
        
        //convert instance to a ds_map
        var instanceMap = ds_map_create();
        var list1 =  ds_list_create();
        //
            
        ds_map_add(instanceMap, "timer", instance.timer);
        ds_map_add(instanceMap, "global-max_number_of_brains", global.max_number_of_brains);   
        ds_map_add(instanceMap, "global-number_of_brains_on_screen", global.number_of_brains_on_screen);
        ds_map_add(instanceMap, "global-creating_brain", global.creating_brain);
        
        ds_map_add(instanceMap, "global-frank_timer", global.frank_timer);
        ds_map_add(instanceMap, "global-delay_between_brains", global.delay_between_brains);
        ds_map_add(instanceMap, "frank_audio_timer", instance.frank_audio_timer);
        ds_map_add(instanceMap, "timer", instance.sprite_index);
        ds_map_add(instanceMap, "door", instance.door);
        ds_map_add(instanceMap, "door_timer", instance.door_timer);
        ds_map_add(instanceMap, "sprite_index", instance.sprite_index);
        
        ds_map_add(instanceMap, "game_win_flag", instance.game_win_flag);
        ds_map_add(instanceMap, "f_timer", instance.f_timer);
        ds_map_add(instanceMap, "g_timer", instance.g_timer);
        ds_map_add(instanceMap, "f_flag", instance.f_flag);
        ds_map_add(instanceMap, "global-current_level", global.current_level);

        ds_map_add(instanceMap, "global-you_lose", global.you_lose);
        ds_map_add(instanceMap, "random_y", instance.random_y);


        ds_map_add(instanceMap, "frank_audio_timer", instance.frank_audio_timer);
        ds_map_add(instanceMap, "f_timer", instance.f_timer);
        ds_map_add(instanceMap, "g_timer", instance.g_timer);
    


        //ds_list_add(list1, object_get_name(FrankensteinIdleObject), FrankensteinIdleObject.object_index);
        ds_map_add_list(instanceMap, "list", list1);
        //ds_list_add(list1, object_get_name(CastleDoorObject), CastleDoorObject.object_index);
        //ds_list_add(list1, object_get_name(LaserBrainObject), LaserBrainObject.object_index);



        //convert this map into json
        JSONInstance = json_encode(instanceMap);
        
        file_text_write_string(saveFile, JSONInstance);
        
        //seperate with pipe character
        file_text_write_string(saveFile, "|");
        
        ds_map_destroy(instanceMap);       
        ds_list_destroy(list1);



}

///close the file
file_text_close(saveFile);


///////
 
T

Taddio

Guest
You're trying to add an existing, but empty list to the map, maybe that's part of the problem.
Also, destroying the map will automatically destroy the list within it
 
Top