• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM help with saving/loading object

A

Anton Provid

Guest
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.
 

Shavv

Member
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?
 
A

Anton Provid

Guest
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.
 

Yal

🐧 *penguin noises*
GMC Elder
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?
 
A

Anton Provid

Guest
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?
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?
 

Yal

🐧 *penguin noises*
GMC Elder
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?
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))
 
A

Anton Provid

Guest
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))
Thank for helping. i will try it
 
Top