GM:S 1.4 help with saving/loading object

Discussion in 'Programming' started by Anton Provid, Jun 11, 2019.

  1. Anton Provid

    Anton Provid Member

    Joined:
    Jun 11, 2019
    Posts:
    14
    hi, i'm doing the survival project with using GM:S 1.4. In my project, i have trouble when saving and loading the project that mean's i want to building the object (house) then click save and when i loaded the game, the object (house) is not exist. Any help?
    Saving:
    Code:
    ini_open("saveData.ini");
    ini_write_real("house", "obj_house.x", obj_house.x);
    ini_write_real("house", "obj_house.y", obj_house.y);
    ini_close();
    loading game:

    Code:
    ini_open("saveData.ini");
    obj_house.x= ini_read_real("house","obj_house.x",obj_house.x);
    obj_house.y= ini_read_real("house","obj_house.y",obj_house.y);
    ini_close();
    Sorry I'm just newbie so I don't know my coding is correct or not.
     
  2. Shavv

    Shavv Member

    Joined:
    Aug 3, 2016
    Posts:
    53
    Well, if the object doesn't exist, shouldn't you make a check to see if the instance exists? And then create one when needed on the load script?
     
  3. Anton Provid

    Anton Provid Member

    Joined:
    Jun 11, 2019
    Posts:
    14
    Thank @Shavv , i already check it, the instance exists but it didn't saving and loading. I can give the example: in my game, i craft item (obj_house) and then i build obj_house in game. I'm saving game, everything okay and then loading: obj_house does not appear (that means item (obj_house) not loading). I want when loading: obj_house will appear but it isn't working.
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,460
    Do you create the house before setting its X/Y? You just read the variables from the file but it won't do anything if the instance doesn't exist.

    ...also, this approach will only work if you have 1 house, how are you gonna do when you want to have more than one house?
     
  5. Anton Provid

    Anton Provid Member

    Joined:
    Jun 11, 2019
    Posts:
    14
    no, i just create the house when i craft it and place it in room (game). i will create more house so if i have more 3 houses i can use this approach or i have to use another approach? could you please help me with the best approach this can help me create more than 1 house?
     
  6. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,460
    Honestly, I'd say text or binary files are the best way to do this, since INI files require you to hardcode a lot of stuff (and the data is in a human readable form so it's trivial for the player to edit it with a text editor!).

    So, this is what you'd do:
    Code:
    //Save
    var f = file_text_open_write("data.dat");
    with(all){
      file_text_write_real(f,x); file_text_writeln(f);
      file_text_write_real(f,y); file_text_writeln(f);
      file_text_write_real(f,object_index); file_text_writeln(f);
    }
    file_text_close(f)
    
    
    
    //Load
    var f = file_text_open_read("data.dat");
    while(!file_text_eof(f)){
      xx = file_text_read_real(f); file_text_readln(f);
      yy = file_text_read_real(f); file_text_readln(f);
      oo = file_text_read_real(f); file_text_readln(f);
      instance_create(xx,yy,oo);
    }
    file_text_close(f)
    
    Note that this approach saves EVERY object in the room, including menu and effects, and it doesn't save their variables like money. So you'll need to adapt the script a bit to suit your needs. But that's the core idea. (We use a WITH loop over ALL objects to save everything, basically, and then we read from the file until we reach the last line (EOF = End of File))
     
    Anton Provid likes this.
  7. Anton Provid

    Anton Provid Member

    Joined:
    Jun 11, 2019
    Posts:
    14
    Thank for helping. i will try 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