1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 Need help with faulty save/load system.

Discussion in 'Programming' started by pixeltroid, Mar 1, 2019.

  1. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    I have a basic save/load feature that enables me to load game from the last room that he was in. It saves the status of bosses killed, doors open and keys collected. It works perfectly except for one major flaw:

    when I load a game, the player spawns a little away from where he was when the game was saved (by selecting "save" from the pause menu). This results in player spawning in the floor or a wall, thus making the game unplayable!

    Here's my save game script:

    Code:
    with (argument0){
        ini_open("save.ini");  
        roomAlias = "1A" //default room
      
    //SAVE Room
        if (room=room1A) roomAlias = "1A"
        if (room=room1B) roomAlias = "1B"
        if (room=room1C) roomAlias = "1C"
        //and so on
      
    //ini_write_real("SaveFile1", "LoadedRoom",roomAlias);
        ini_write_string("SaveFile1", "LoadedRoom",roomAlias);
      
        ini_write_real("SaveFile1", "playerx",x);
        ini_write_real("SaveFile1", "playery",y);
      
     ini_close();
    }
    
    Here is my load game script:

    Code:
    if file_exists("save.ini")
    {
        ini_open("save.ini");
        LoadedRoom=room_1A; //default room
        roomAlias=ini_read_string("SaveFile1", "LoadedRoom", "1A");
    
    //rooms  
        if (roomAlias=="1A") LoadedRoom = room1A
        if (roomAlias=="1B") LoadedRoom = room1B
        if (roomAlias=="2A") LoadedRoom = room2A
        //and so on  
     
         playerx=ini_read_real("SaveFile1", "playerx", 10);
         playery=ini_read_real("SaveFile1", "playery", 10);
     
        ini_close();
        instance_create(playerx, playery, obj_player);
      
        room_goto(LoadedRoom);
    }
    else
    {
        //do nothing
    }
    
    How can I fix this?

    ---------------------------------------------------------------------

    Also, is there a way to change my script so that the loaded game makes player starts in the ENTRANCE of the room where he respawns after he dies? Because each room has a very specific starting point established in a room change object.

    For example: If I have to go to a room, player needs to touch a door object with this code:

    Code:
    //go thru the door
    instance_create(x,y,obj_fade)
    if (room_exists(other.new_room)) {
        room_goto(other.new_room);
        x = other.new_x;
        y= other.new_y;
    
    spawnx = other.new_x;
    spawny = other.new_y;
    }
    
    The same door object contains a creation code in the room editor:

    Code:
    new_room = room1B;
    new_x = 34
    new_y = 185
    
    spawnx = other.new_x;
    spawny = other.new_y;
    

    Any help will be appreciated!
     
  2. roozilla

    roozilla Member

    Joined:
    Feb 4, 2017
    Posts:
    111
    You have everything you need don't you? Youve recorded your players last X and Y Coordinate. Are you generating the rooms objects first and then yoir player last? If you need to spawn him at each rooms entrance just store the start points for each room, if the player saved in the last room read your text files stored value for that rooms x and y
     
    Last edited: Mar 1, 2019
  3. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    Im not sure I follow?

    "If you need to spawn him at each rooms entrance just store the start points for each room"

    How do I do that? Can you show me by an example?

    Presently, room start points are stored in the creation code of a room change object thats placed in the previous room (from where the player enters)
    Code:
    new_room = room1B;
    new_x = 34
    new_y = 185
    
    spawnx = other.new_x;
    spawny = other.new_y;
    
     
  4. roozilla

    roozilla Member

    Joined:
    Feb 4, 2017
    Posts:
    111
    You are using game makers write and write to/from a text file, you need to read those documents again for your load/save if you dont fulluly understand them. If you write to a text file take a look at the actual text file see how your values are getting set.
     
  5. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    You could do it in a 2D array
    Code:
    spawn_point[0,0]= rm_forest;. //Room name
    spawn_point[0,1]= 32;   //X position
    spawn_point[0,2]= 512; //y posotion
    
    ///So on for all rooms
    Then you do a room check (for loop) with spawn_point[i,0] and spawn your player at (spawn_point[i,1],spawn_point[i,2]) as (x,y) coordinates
     
  6. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    OK. Thats a little beyond my coding abilities. o_O

    is there any way I could just tweak my existing code? The thing works...its just that the "load game" script spawns my player about a 100 pixels to the left of where he is supposed to spawn.
     
  7. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    Arrays are super simple and REAAAAALLY useful for a LOT of stuff. Readeable, convenient, easily tweakeable, definately read and practice them, you'll be glad you did.

    As for your code, are you sure it actually save/reads your position to your file, and not just reading a default value (10 in your case, it seems).
    Save a game and check your actual ini file to see if it saves correctly, first!
     
  8. roozilla

    roozilla Member

    Joined:
    Feb 4, 2017
    Posts:
    111
    Follow taddio's advice, also read the documents. Your issue is probably that your init_write_real for your playerX and playerY. Is the x and y reference you use for those functions located in your player object or a game saving object? If so you are saving the objects x and y that that code is in
     
  9. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    I still dont know what to do. :/
     
  10. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    Allow me to quote myself
     
  11. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    I checked the INI file. Everything is saving correctly. Except the players save position is not being saved at the exact position he was in when I save it. Instead his coordinates are shifted upwards or to the left.
     
  12. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    So that means it's not savibg correctly.
    - Are you calling scr_save from the obj_player, or another object? You're saving x and y, and not obj_player.x, it won't work
    -Also, check if file exists before saving/loading, and you can delete section you're going to write to, otherwise it may pile-up in the .ini, it hapoened to me (I had many player_name in the same section, for example)
    - From what you posted, you're using other.new_x and y in the room creation code, and outside of a with(), so that also very likely (read: definately) will break.
    - There's also many syntax things that makes me go "hummm". I would use a switch or at least "else if" instead of a simple "if" in this part you have there
    Code:
    //rooms 
        if (roomAlias=="1A") LoadedRoom = room1A
        if (roomAlias=="1B") LoadedRoom = room1B
        if (roomAlias=="2A") LoadedRoom = room2A
        //and so on 
    Check these, plus the docs and forums, and you'll get it. But as is, I can't say I'm surprised it doesn't do what you expect.
     
  13. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    its being called from the pause menu object -- that gives player the option to save mid game.

    so how do i fix that?

    i tried "else if" but im still getting the same error!
     
  14. roozilla

    roozilla Member

    Joined:
    Feb 4, 2017
    Posts:
    111
    Your original post's first block of code is using:
    ini_write_real("SaveFile1", "playerx",x);
    ini_write_real("SaveFile1", "playery",y);

    So you are writing the x and y values of the object that is calling this code.

    Think of it like this, I add a obj_player and another object called obj_savegame.

    now I create an instance of these two objects, or in your case you may have simply added them in the room through the room editor.
    When you start up your game and call save through obj_savegame and it hits:

    ini_write_real("SaveFile1", "playerx",x);
    ini_write_real("SaveFile1", "playery",y);

    It is saying give me the x and y coordinate of obj_savegame not for obj_player and write them to SafeFile1.
    If you chose to separate the save code from the player object I would suggest instance_find. To make things simple just use:

    var player = instance_find(obj_player, 0);
    ini_write_real("SaveFile1", "playerx", player.x);
    ini_write_real("SaveFile1", "playery", player.y);

    Keep in mind this is just a simple solution to your problem, this doesn't take into account if you end up have multiple players in the room or decide to want to change it to have the player instead to load at the entrance of the room. If you do not fully understand how the code you are using works then you should really watch more tutorials or read the documentation to find out what it is exactly doing. Also be sure to change your references to fit the names you made them with.
     
    Taddio likes this.
  15. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    669
    That code didnt work.

    But I found a way around it. I added a save checkpoint after watching a Shaun Spalding tutorial. His code required me to use a variable called "global.checkpoint" which saves the players exact coordinates.

    So I changed your code to

    Code:
     var player = instance_find(obj_player, 0);
    ini_write_real("SaveFile1", "playerx", global.checkpointx);
    ini_write_real("SaveFile1", "playery", global.checkpointy);
    and placed the checkpoint object at the start of each room. So each time I load the game via the "load game" script, it reads the players position as stored by the checkpoint code. Or so I think.

    Thanks you for your help. And thanks to @Taddio too! :)
     

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