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

GML [SOLVED] Saving/Loading across different game versions doesn't load up correct values

I have a save/load system. Saving/loading works on the same game version but when I try to load a saved file from a previous version of my game, the values being loaded in is different for some reason. I have no idea why.

So, instead of loading your player character for example, it will load an enemy unit instead.


This is how I am saving:

Code:
var save_data = ds_map_create();

save_data[? "playerName0"] = global.playerName[0];

var save_string = json_encode(save_data);
ds_map_destroy(save_data);
save_string = base64_encode(save_string);

var file = file_text_open_write(working_directory + argument0);
file_text_write_string(file, save_string);
file_text_close(file);

This is how I am loading:

Code:
var file = file_text_open_read(working_directory + argument0);
var save_string = file_text_read_string(file);

file_text_close(file);

save_string = base64_decode(save_string);
var save_data = json_decode(save_string);

if (!is_undefined(ds_map_find_value(save_data, "playerName0"))) {
    global.playerName[0] = save_data[? "playerName0"];
}
Is there a way to make a save/load system work across all future versions of your game?
 
Last edited:
So is player name a string that the player has typed for their name? Or is it the name of the player object? When you're saving any asset types (sprites, objects, etc), you can't save them by direct reference. For instance:
Code:
save_data[? "player"] = obj_player;
That will not work properly between different versions. This is because it simply saves the object type as what number that object currently is in the asset list. So let's say it's 45. If later on, you create a new object and move it somewhere before the player object, the player object number will get bumped up to 46, and when you try to create the player object from the saved data (which is number 45, even though the player object is now actually 46) it will load whatever object has 45 as it's index. The way to get around this is:
Code:
save_data[? "player"] = object_get_name(obj_player);
This will store the actual object name as a string, "obj_player" in this case, rather than as an index number. Then later on when you want to create the player object you do the following:
Code:
var player_object = asset_get_index(save_data[? "player"]);
instance_create_layer(x,y,"layer",player_object);
Asset_get_index looks for whatever object is named after the string provided (which you saved previously: "obj_player") and will return the correct asset index for that object. The same methodology applies to all other assets (sprites for example).
 
Thank you!!!!

I have been trying to figure this out for weeks. I had a feeling it was loading the index of the objects and the index was changing between versions but I just didn't know what the solution was for this. I also didn't know that saving the actual asset types was the issue. (I am not too knowledgeable on save/load systems and I didn't know about the asset index functions.) I tried googling a problem similar to this but I just couldn't find anything.

You taught me quite a lot, I seriously appreciate it! :D

Super happy this is finally solved!
 
Top