1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Saving and Loading a Persistent object (INI)

Discussion in 'Programming' started by Josef Scroggins, Apr 4, 2018.

  1. Josef Scroggins

    Josef Scroggins Member

    Joined:
    Jan 12, 2018
    Posts:
    23
    Edit #2: I thought I had this figured out but now the player's coordinates don't change and the camera won't snap onto the player.

    //Loading
    Code:
    CL = objCallList;
    
    if file_exists("Save.helix")
    {
        ini_open("Save.helix");
        var LoadedRoom = ini_read_real("Room","Room",Overworld0);
        
        CL.hp = ini_read_real("Player","HP",20);
        CL.maxhp = ini_read_real("Player","MAXHP",20);
        CL.xp = ini_read_real("Player","XP",0);
        CL.maxxp = ini_read_real("Player","MAXXP",10);
        CL.money = ini_read_real("Player","Cash",0);
        CL.lvl = ini_read_real("Player","Level",1);
        CL.skill = ini_read_real("Player","Skill",0);
        CL.cashgainmax = ini_read_real("Luck","CGMX",3);
        CL.cashgainmin = ini_read_real("Luck","CGMN",0);
        CL.xpgainmax = ini_read_real("Luck","XPGX",5);
        CL.xpgainmin = ini_read_real("Luck","XPGN",1);
        CL.statsMenu = ini_read_real("Misc","Stat Menu",false);
        var PLX = x = ini_read_real("Player","PX",x)
        var PLY = y = ini_read_real("Player","PY",y)
    
        ini_close();
        
        if !instance_exists(plrHero0)
        {
            instance_create_layer(PLX,PLY,"Instances",plrHero0);
            instance_create_layer(PLX,PLY,"Instances",objDraw);
            instance_create_layer(PLX,PLY,"Instances",objCamera);
            with(ev_game_start)
            {
                plrHero0.x = PLX;
                plrHero0.y = PLY;
            }
            instance_activate_object(plrHero0);
            CL.hascontrol = true;
        }
        room_goto(LoadedRoom);
    }
    else
    {
        show_message("There is no available save data!");
    }
    

    Original Post:

    Hello everyone, I have been stuck on this topic for a while. I have saved all the variable and the players location and room. When I try and load the game it crashes or there isn't an instance of the player. Any help would be wonderful.

    Code:
    CL = objCallList;
    
    ini_open("Save.helix");
    
    var SavedRoom = room;
    ini_write_real("Player","HP",CL.hp);
    ini_write_real("Player","MAXHP",CL.maxhp);
    ini_write_real("Player","XP",CL.xp);
    ini_write_real("Player","MAXXP",CL.maxxp);
    ini_write_real("Player","Cash",CL.money);
    ini_write_real("Player","Level",CL.lvl);
    ini_write_real("Player","Skill",CL.skill);
    ini_write_real("Luck","CGMX",CL.cashgainmax);
    ini_write_real("Luck","CGMN",CL.cashgainmin);
    ini_write_real("Luck","XPGX",CL.xpgainmax);
    ini_write_real("Luck","XPGN",CL.xpgainmin);
    ini_write_real("Misc","Stat Menu",CL.statsMenu);
    ini_write_real("Room","Room#",SavedRoom);
    
    ini_close();
    

    Code:
    CL = objCallList;
    if file_exists("Save.helix")
    {
        ini_open("Save.helix");
        var LoadedRoom = ini_read_real("Room","Room#",-1);
      
        CL.hp = ini_read_real("Player","HP",20);
        CL.maxhp = ini_read_real("Player","MAXHP",20);
        CL.xp = ini_read_real("Player","XP",0);
        CL.maxxp = ini_read_real("Player","MAXXP",10);
        CL.money = ini_read_real("Player","Cash",0);
        CL.lvl = ini_read_real("Player","Level",1);
        CL.skill = ini_read_real("Player","Skill",0);
        CL.cashgainmax = ini_read_real("Luck","CGMX",3);
        CL.cashgainmin = ini_read_real("Luck","CGMN",0);
        CL.xpgainmax = ini_read_real("Luck","XPGX",5);
        CL.xpgainmin = ini_read_real("Luck","XPGN",1);
        CL.statsMenu = ini_read_real("Misc","Stat Menu",false);
    
        ini_close();
      
        room_goto(LoadedRoom);
    }
    else
    {
        show_message("There is no available save data!");
    }
    
     
    Last edited: Apr 5, 2018
  2. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    959
    Do you actually create the player prior to calling the load script?
     
  3. Josef Scroggins

    Josef Scroggins Member

    Joined:
    Jan 12, 2018
    Posts:
    23
    Sorry, I just re-added the X and Y coordinates. When do I create the instance? I tried it when I revised the Load script it crashed because the var I was calling was farther down the script. Would I put the creation in a game start event?


    Code:
    CL = objCallList;
    
    instance_create_layer(PLX,PLY,"Instances",plrHero0)
    if file_exists("Save.helix")
    {
        ini_open("Save.helix");
        var LoadedRoom = ini_read_real("Room","Room#",-1);
        
        CL.hp = ini_read_real("Player","HP",20);
        CL.maxhp = ini_read_real("Player","MAXHP",20);
        CL.xp = ini_read_real("Player","XP",0);
        CL.maxxp = ini_read_real("Player","MAXXP",10);
        CL.money = ini_read_real("Player","Cash",0);
        CL.lvl = ini_read_real("Player","Level",1);
        CL.skill = ini_read_real("Player","Skill",0);
        CL.cashgainmax = ini_read_real("Luck","CGMX",3);
        CL.cashgainmin = ini_read_real("Luck","CGMN",0);
        CL.xpgainmax = ini_read_real("Luck","XPGX",5);
        CL.xpgainmin = ini_read_real("Luck","XPGN",1);
        CL.statsMenu = ini_read_real("Misc","Stat Menu",false);
        var PLX = plrHero0.x = ini_read_real("Player","PX",plrHero0.x)
        var PLY = plrHero0.y = ini_read_real("Player","PY",plrHero0.y)
    
        ini_close();
        
        room_goto(LoadedRoom);
    }
    else
    {
        show_message("There is no available save data!");
    }
    
     
  4. Josef Scroggins

    Josef Scroggins Member

    Joined:
    Jan 12, 2018
    Posts:
    23
    My problem now is that when I load the room none of the objects appear. I ran a debug message to tell me if the player existed it did but wouldn't show up on screen. I revised the load script again for this.

    Code:
    CL = objCallList;
    
    if file_exists("Save.helix")
    {
        ini_open("Save.helix");
        var LoadedRoom = ini_read_real("Room","Room",Overworld0);
        
        CL.hp = ini_read_real("Player","HP",20);
        CL.maxhp = ini_read_real("Player","MAXHP",20);
        CL.xp = ini_read_real("Player","XP",0);
        CL.maxxp = ini_read_real("Player","MAXXP",10);
        CL.money = ini_read_real("Player","Cash",0);
        CL.lvl = ini_read_real("Player","Level",1);
        CL.skill = ini_read_real("Player","Skill",0);
        CL.cashgainmax = ini_read_real("Luck","CGMX",3);
        CL.cashgainmin = ini_read_real("Luck","CGMN",0);
        CL.xpgainmax = ini_read_real("Luck","XPGX",5);
        CL.xpgainmin = ini_read_real("Luck","XPGN",1);
        CL.statsMenu = ini_read_real("Misc","Stat Menu",false);
        var PLX = x = ini_read_real("Player","PX",x)
        var PLY = y = ini_read_real("Player","PY",y)
    
        ini_close();
        
        room_goto(LoadedRoom);
        if !instance_exists(plrHero0)
        {
            instance_create_layer(PLX,PLY,"Instances",plrHero0)
        }
    }
    else
    {
        show_message("There is no available save data!");
    }
    
     
  5. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,167
    Room_goto() doesn't happen until the end of the script.

    So the script will run, create the player then move to the next room.

    If your player is not persistent, it is being created in the loading room, then destroyed when the room change happens.
     
  6. Josef Scroggins

    Josef Scroggins Member

    Joined:
    Jan 12, 2018
    Posts:
    23
    So where do I create the player and when?
     
  7. Josef Scroggins

    Josef Scroggins Member

    Joined:
    Jan 12, 2018
    Posts:
    23
    I thought I had this figured out but now the player's coordinates don't change and the camera won't snap onto the player.
    Code:
    CL = objCallList;
    
    if file_exists("Save.helix")
    {
        ini_open("Save.helix");
        var LoadedRoom = ini_read_real("Room","Room",Overworld0);
        
        CL.hp = ini_read_real("Player","HP",20);
        CL.maxhp = ini_read_real("Player","MAXHP",20);
        CL.xp = ini_read_real("Player","XP",0);
        CL.maxxp = ini_read_real("Player","MAXXP",10);
        CL.money = ini_read_real("Player","Cash",0);
        CL.lvl = ini_read_real("Player","Level",1);
        CL.skill = ini_read_real("Player","Skill",0);
        CL.cashgainmax = ini_read_real("Luck","CGMX",3);
        CL.cashgainmin = ini_read_real("Luck","CGMN",0);
        CL.xpgainmax = ini_read_real("Luck","XPGX",5);
        CL.xpgainmin = ini_read_real("Luck","XPGN",1);
        CL.statsMenu = ini_read_real("Misc","Stat Menu",false);
        var PLX = x = ini_read_real("Player","PX",x)
        var PLY = y = ini_read_real("Player","PY",y)
    
        ini_close();
        
        if !instance_exists(plrHero0)
        {
            instance_create_layer(PLX,PLY,"Instances",plrHero0);
            instance_create_layer(PLX,PLY,"Instances",objDraw);
            instance_create_layer(PLX,PLY,"Instances",objCamera);
            with(ev_game_start)
            {
                plrHero0.x = PLX;
                plrHero0.y = PLY;
            }
            instance_activate_object(plrHero0);
            CL.hascontrol = true;
        }
        room_goto(LoadedRoom);
    }
    else
    {
        show_message("There is no available save data!");
    }
    
    I'm really confused now and not sure what to do about it. I need to fix this ASAP so I can keep working on bug fixes.
     

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