• 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!

Saving an entire room while ingame

Greetings,

lets say i wanted to save a game with all the objects in the precise location and with all the stats for each object, how would i go about doing this?

i have a system to get halfway there already working, i can take the objects, create a dot object, load all the critical data into that dot object and then based upon its relative position from an axis point be able to then move the axis to a new location and then have those dots go and unload all their information in the new location but what i basically end up with is a couple dozen objects on a map.

what i need to do is take all the information from all those dots, reguardless (relatively speaking there will be a maximum number) of how many there are and then load all of the information into a single file which can then be read by the program at a later date to recreate the room.

ideally the point of this will be to then allow the player to upload this file to a server so they can share it with friends but im getting ahead of myself, for right now though im just curious about how you would get game maker to create a list so that you could have information for all the objects in a single location.

thank you.
 

GMWolf

aka fel666
I think you are overcomplicating this.
What extra functionality does the dot object provide? It seems like you are adding some useless steps.

Just loop trhough all the objects like so:
Code:
with(all)
{
  //write stuff to file
}
If different objects need to save different things, create a saving script for each object, and assign it in the create event. For exams, in the player object, you may have:
save_script = scr_player_save;

scr_player_save may then write first the object being saved, the x and y position, and health, perhaps.

scr_block_save may just have block object, followed by x and y.

Then, in your save loop, just write:
Code:
with(all)
{
  execute_script(save_script); //could well be script_execute...
}
To load, do something similar: assign a load script to all objects. Then read the file sequentially. First read an object name, create it, and call the load script with it. Hopefully it reads as many values as it saved and the next value is an object name again


Good luck :)
 
Thank you for the responce fel666, i have tried to wrap myhead around this and so far am having no luck.

the dot object im using is to safely re orientate a screen from landscape to portrait so everything moves in a single step and maintaining the everything in proportion, some of the data from such a thing would be needed so that all the objects appear in the correct location when a file loads when the question is not how big the room is but where you want everything to be in relation to.

anyhow i dont understand how you would actually create the script itself.

lets say i have an object, obj_bob, and bob has a set of variables like its x and y position, its colour and its angle.

now i put in an alarm (or have a line of code that says
Code:
with (obj_bob)
{
duck duck duck
}
how would i translate the words duck duck duck so that in the script it would read out

Code:
bob = instance_create (ax,ay,obj_bob)
bob.colour = 2
bob.image_angle = 90
even if there are 1 or 50 obj_bob?

if i could figure out how to do that then i believe i could find a simple way to save the file so it could be loaded later but im still lost about how to translate the presence of an object into creating command functions in a script to spawn an object with the identical information.

Thank you very much for the help so far. :)
 

GMWolf

aka fel666
ok, this shouldnt be too hard...
I really recomend you use maps, and json to save and load. Its by far the easiest, cleanest wat to save/load data.

Lets say you want to save all obj_bob objects, and all obj_homer objects:
Code:
//Step 1: Create a map. This map will be what ends up holding all our data
var map = ds_map_create();

//Step 2: Create lists that will hold all our objects. Create one per object type
var bob_list = ds_list_create();
var homer_list = ds_list_create();

//Step 3: Add those lists to the map. We use ds_map_add_list() to tell GM this is a list we are adding, not just a number. This is used hen dealing with JSon.
ds_map_add_list(map, "bobs", bob_list);
ds_map_add_list(map, "homers", homer_list);

//Step 4: Go through all bob, then homer objects and add them to the lists. We will represent them with maps
//Lets start with all bob objects
with(obj_bob) {
    //Step 4.1: Create a map to represent this instance
    var instance_map = ds_map_create(); //This map represetns this instance
    ds_map_add(instance_map, "x", x); //Store the x variable
    ds_map_add(instance_map, "y", y); //Store the y variable
    ds_map_add(instance_map, "image_angle", image_angle); //Store the image angle variable

    //Step 4.2: Add that map to bob_list. There is no ds_list_add_map, so we have to use mark_as_map
   var pos = ds_list_size(parent);
   ds_list_add_map(bob_list, instance_map); //Add the map index
   ds_list_mark_as_map(bob_list, pos);
}

//Lets add homer objects now
with(obj_bob) {
    //Step 4.3: Create a map to represent this instance
    var instance_map = ds_map_create(); //This map represetns this instance
    ds_map_add(instance_map, "duck", duck); //Store the z variable
    ds_map_add(instance_map, "z", z); //Store the z variable

    //Step 4.4: Add that map to homer_list. There is no ds_list_add_map, so we have to use mark_as_map
   var pos = ds_list_size(parent);
   ds_list_add_map(homer_list, instance_map); //Add the map index
   ds_list_mark_as_map(bob_list, pos);
}

//Step 5: Create JSon and save it
var json = json_encode(map);
var file = file_text_open_write(working_directory + "\save.json");
file_text_write_string(file, map);
file_text_close(file);

//Step 6: Free the map, along with all its children
ds_map_destroy(map);
I recomend you go cehck out what the .json file looks like. Any text editor like notepad should be able to open it. I recoment notepad ++.
Now lets look at how we can load this:
Code:
///Step 1: Load the json.
var file = file_text_open_read(working_directory + "\save.json");
var json= "";
while (!file_text_eof(file)) {
    json+= file_text_read_string(file);
    file_text_readln(file);
}
file_text_close(file);

//Step 2: Decode the json into a map
var map = json_decode(json);
//Step 3: get the object lists
var bob_lists = ds_map_find_value(map, "bobs");
var homer_list = ds_map_find_value(map, "homers");

//Step 4: Go through the list, creating the objects:
//lets start with bobs
for(var i = 0; i < ds_list_size(bob_list); i++) {
    var instance_map = ds_list_find_value(bob_list, i); //Get the map representing this instance
    var instance = instance_create(0, 0, obj_bob);
    instance.x = ds_map_find_value(instance_map, "x");
    instance.y = ds_map_find_value(instance_map, "y");
   instance.image_angle = ds_map_find_value(instance_map, "image_angle");
}

//Now lets deal with homers
for(var i = 0; i < ds_list_size(homer_list); i++) {
    var instance_map = ds_list_find_value(homer_list, i); //Get the map representing this instance
    var instance = instance_create(0, 0, obj_homer);
    instance.duck = ds_map_find_value(instance_map, "duck");
    instance.z = ds_map_find_value(instance_map, "z");
}
//Step 6: free the map
ds_map_destroy(map);
This should be all you need. If you need to store the created instances somehow, it shouldnt be too tricky to modify this code.
As you can see, this will quickly create a hell of script. This is why i suggest you create scripts for each object, that return the instance map.

This has given me the idea to work on a saving system though. Will probably publish on the market place. If it ends up any good i will post a link here :)
 
Last edited:
Thank you very much for going into so much detail, please do let me know if you do put something like this in the market place, presuming this all works i will let you know as soon as it does.

thank you.
 

GMWolf

aka fel666
Thank you very much for going into so much detail, please do let me know if you do put something like this in the market place, presuming this all works i will let you know as soon as it does.

thank you.
No problem :)
quick disclaimer: All this code is pretty long, and i havent tested it one bit. Dont be supprised if you have a few errors to fix :)
 
Top