1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

[SOLVED]Save procedure

Discussion in 'Programming' started by Ted Gress, Mar 12, 2019.

  1. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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
     
  2. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    586
    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).
     
  3. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    826
    I find it fascinating that the code shown has a huge amount of entries to write, none of which use the for loop variable.
     
  4. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    @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.

    What exactly do you mean? Could you elaborate? Also, I'm running the save script and its not writing to a file?
     
  5. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    586
    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.
     
  6. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    826
    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?
     
  7. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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
    
     
  8. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    826
    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.
     
  9. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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: Mar 17, 2019
  10. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    Does anybody have any suggestiosn on how to solve/debug this issue?
     
  11. SeraphSword

    SeraphSword Member

    Joined:
    Aug 16, 2016
    Posts:
    17
    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.
     
    IndianaBones likes this.
  12. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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);
     
  13. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    Ok. I'm completely lost as to why this isn't writting to disk and how to figure out the problem.
     
  14. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    500
    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: Mar 23, 2019
  15. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    826
    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.
     
  16. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    Where are these save files save to anyway? I checked %appdata% and didn't find anything/
     
  17. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,054
    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.
     
  18. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    Ok. So I checked the working directory and my test save file is there....but (*face-palm*) the file is completely empty.
     
  19. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    500
    How did you confirm that it is empty?
     
  20. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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.
     
  21. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    500
    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", "")?
     
  22. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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.
     
  23. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    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.
     
  24. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    500
    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?
     
  25. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    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
     
    Pfap likes this.
  26. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    Mine is in C:\users\appdata\local\BatsVSBrains
    Is that what you mean by hidden folder?
     
  27. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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?
     
  28. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,054
    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.
     
    Pfap likes this.
  29. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    826
    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 }
    
     
    Pfap, Ted Gress and IndianaBones like this.
  30. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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.
     
  31. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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?
     
  32. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    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.
     
  33. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    @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
     
  34. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    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?
     
  35. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
  36. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    This is when it freezes.
     
  37. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    upload_2019-3-28_12-26-26.png This is when it freezes
     
  38. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    When it freezes:
     
  39. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    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
     
  40. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    Frozen Game
     

    Attached Files:

  41. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    More Profiler instances.
     

    Attached Files:

  42. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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.
     
  43. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    775
    You're supposed to pass a ds_map to the json_encode function, not a ds_list.
     
  44. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    Totally unrelated to your saving problem, but dude, from the looks of it, I think we smoke the same stuff.
     
  45. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    Lol yeah! We should collaborate on something sometime
     
    Taddio likes this.
  46. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    What do I pass a ds list to?
     
  47. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    775
  48. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    Just hit me up on the next jam :)
     
    Ted Gress likes this.
  49. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    628
    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);
    
    
    ///////
     
  50. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice