Toulhane
Member
Hi,
I followed this Shaun Spalding tutorial to try to implement a saving system in my game while adapting it a bit :
My game being a card game, I wanted to save my card inventory which is a ds map (I should probably have made it as a ds list instead but I don't think that's the problem here).
Here is the code I use :
In my obj_master "press s" event I have this to save my cards inventory
And in my obj_master "press l" event I have this to load what I saved
Here is the script used in the save event to encode all of this with JSON :
And here is the script used in the load event to decode this file :
The problem is that everything runs just fine, except that I have the correct number of entries in my loaded "deck" ds map (if I saved while having 6 cards in the deck, there indeed 6 entries in the ds map once loaded), but all the entries are undefined.
I've done some testing and everything is good until the JSON script is ran, I have the correct number of entries and each entry contains a card name. But once loaded, all entries are suddenly undefined.
I have to say that I have pretty much no idea what those JSON script taken from the tutorial actually do, so I'm pretty helpless at figuring out what is going wrong.
This is really frustrating and I would really appreciate any help.
And just in case : I know I should destroy my data structure once it is saved, but I didn't do it for practicality reasons during testing and even if I actually destroy it, the problem remains.
I followed this Shaun Spalding tutorial to try to implement a saving system in my game while adapting it a bit :
Here is the code I use :
In my obj_master "press s" event I have this to save my cards inventory
GML:
// create root list
var _root_list = ds_list_create();
// save deck into root list
//ds_list_add(_root_list, obj_deck.deck);
var deck_copy = ds_map_create();
ds_map_copy(deck_copy, obj_deck.deck);
ds_list_add(_root_list, deck_copy);
ds_list_mark_as_map(_root_list, ds_list_size(_root_list)-1);
// wrap root list in a map
var wrapper = ds_map_create();
ds_map_add_list(wrapper, "root", _root_list);
// save in a file as string
var _string = json_encode(wrapper);
scr_save_string_to_file("savegame.sav", _string);
GML:
if (file_exists("savegame.sav") )
{
var wrapper = scr_load_json_from_file("savegame.sav");
var list = ds_map_find_value(wrapper, "root");
var _deck = ds_list_find_value(list, 0);
ds_map_destroy(obj_deck.deck);
obj_deck.deck = ds_map_create();
ds_map_copy(obj_deck.deck, _deck);
}
GML:
function scr_save_string_to_file(argument0, argument1){
var filename = argument0;
var _string = argument1;
var buffer = buffer_create(string_byte_length( _string)+1, buffer_fixed, 1);
buffer_write(buffer, buffer_string, _string)
buffer_save(buffer, filename);
buffer_delete(buffer);
}
GML:
function scr_load_json_from_file(argument0){
var filename = argument0;
var buffer = buffer_load(filename);
var _string = buffer_read(buffer, buffer_string);
buffer_delete(buffer);
var json = json_decode(_string);
return json;
}
I've done some testing and everything is good until the JSON script is ran, I have the correct number of entries and each entry contains a card name. But once loaded, all entries are suddenly undefined.
I have to say that I have pretty much no idea what those JSON script taken from the tutorial actually do, so I'm pretty helpless at figuring out what is going wrong.
This is really frustrating and I would really appreciate any help.
And just in case : I know I should destroy my data structure once it is saved, but I didn't do it for practicality reasons during testing and even if I actually destroy it, the problem remains.