Save procedure

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

  1. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    501
    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:
    549
    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:
    785
    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:
    501
    @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:
    549
    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:
    785
    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:
    501
    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:
    785
    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:
    501
    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 at 10:47 PM

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