• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Legacy GM Adding a save function to my game

F

Flame Alex

Guest
so im making a sandbox game where the player drags blocks from the map and builds with them.
i felt that it would be nice to let people save there work, so i watched a few tutorials, and tried to add a save function. none of them worked. if any one can help me it would be grate.

just to be clear, im trying to save the players position,save the position of any blocks that may have been moved, and make sure when the player loads the game, the destroyed blocks are not there.

thanks to any one that can help!
 
T

Trasoside

Guest
the dumbest way you can do it is by using
game_save(filename)
and
game_load(filename)

if you want to use a more advanced way:
in your case, I would save everything as json using ds_maps and lists.
in order to do that you'll need to understand how ds_maps and lists works and how to use them correctly (look at the manual)

1.you should begin with a main ds_map that contains everything:

Code:
var save = ds_map_create();
2. then, add your player's position to that map:
Code:
save[?"player_x"] = obj_player.x;
save[?"player_y"] = obj_player.y;

3. then, add your blocks to that map:
Code:
var blocks = ds_list_create();
with(obj_block)
{
    var block = ds_map_create();
    block[?"x"] = x;
    block[?"y"] = y;
    ds_list_add(blocks, block);
    ds_list_mark_as_map(blocks, ds_list_size(blocks)-1); // so gm will know to refer it as a map and destroy it later
}
ds_map_add_list(save, "blocks", blocks);
4.save that map:
Code:
ds_map_secure_save(save, filename)
*note: you may want to destroy your map here if you no longer use it.

5.load that map:
Code:
var save = ds_map_secure_load(filename)
6.create your player from that map:
Code:
instance_create(save[?"player_x"], save[?"player_y"], obj_player);
7.create your blocks from that map:

Code:
var blocks = save[?"blocks"];
var sz = ds_list_size(blocks);
for(var i = 0; i < sz; i++)
{
    var block = blocks[|i];
    instance_create(block[?"x"], block[?"y"], obj_block);
}
*note: you may want to destroy your save map here if you no longer use it.
*another note: read in the manual about any function you are not familiar with
*you might also want to use
Code:
show_message(json_encode(save));
to see how your json file would look like
hope this info could help :)
 
Last edited by a moderator:
F

Flame Alex

Guest
i have over 100 different blocks that where used, how should i put them all into the list thing
 
T

Trasoside

Guest
i have a parent block that all the other blocks are from, can i just list that?
in that case, you might want to try something like:

for saving:
Code:
var blocks = ds_list_create();
with(obj_block)
{
    var block = ds_map_create();
    block[?"x"] = x;
    block[?"y"] = y;
    block[?"type"] = object_get_name(object_index);// save block object name in a new key
    ds_list_add(blocks, block);
    ds_list_mark_as_map(blocks, ds_list_size(blocks)-1); // so gm will know to refer it as a map and destroy it later
}
ds_map_add_list(save, "blocks", blocks);
for loading:
Code:
var blocks = save[?"blocks"];
var sz = ds_list_size(blocks);
for(var i = 0; i < sz; i++)
{
    var block = blocks[|i];
    instance_create(block[?"x"], block[?"y"], asset_get_index(block[?"type"]));  // convert object_name to object_index
}
notice, that what i did there was saving the object_name in another key in the map,
then I used that name when I loaded the block to convert it back to object_index and create an instance of it.

your json should look like somthing like this:

Code:
{
    "player_x":"6",
    "player_y":"4",
    "blocks":[
        {
            "type":"obj_block_sand",
            "x":4,
            "y":5
        },
        {
            "type":"obj_block_grass",
            "x":100,
            "y":200
        }
    ]
}
*if you think about it, using the way I saved the blocks, you could actually save any object in your game, including your player

leave a like if it helped :)
feel free to ask any questions.
 
Last edited by a moderator:
Top