FoxyOfJungle
Kazan Games
I am making a customizable game where the player will have to build a mall.
How do I save all instances of 2 different objects and also save global variables such as score and others (All in the same ds_map)?
I need everything to be saved in a ds_map and then use ds_map_write ()
Save/Load Code:
The code above can only save one object and does not save other global variables.
After saved:
Currently: (json_encode)
How I want: (ds_map_write)
I had made another similar code but it was very unstable.
As you can see, I saved it as json, but I just want to use data-structure (ds_map) and save everything in just one string, but it doesn't work. Can anyone help me to implement this code (save many objects and others global variables)?
(I have a lot of experience with GameMaker, since 2012, but this is my first time trying to save everything in a ds_map, I already read the manual but I still can't do it in a practical way. I already looked for several related topics but I can't find it!)
How do I save all instances of 2 different objects and also save global variables such as score and others (All in the same ds_map)?
I need everything to be saved in a ds_map and then use ds_map_write ()
Save/Load Code:
Code:
/// @description Save Level
var _file = "gamedata.dat"
//Create root list
var _root_list = ds_list_create();
//For every instance, create a map
with (obj_par_building)
{
var _map = ds_map_create();
ds_list_add(_root_list,_map);
ds_list_mark_as_map(_root_list,ds_list_size(_root_list)-1);
var _obj = object_get_name(object_index);
ds_map_add(_map, "obj", _obj);
ds_map_add(_map, "x", x);
ds_map_add(_map, "y", y);
}
//Wrap the root LIST up in MAP!
var _wrapper = ds_map_create();
ds_map_add_list(_wrapper, "ROOT", _root_list);
//Save all of this to a string
var _string = json_encode(_wrapper)
//var _string = ds_map_write(_wrapper);
//Save file (or upload to server)
file_delete(_file);
var _f = file_text_open_write(_file);
file_text_write_string(_f, _string);
file_text_close(_f);
Code:
/// @description Load Level
var _file = "gamedata.dat"
if (file_exists(_file))
{
with (obj_par_building) instance_destroy();
//Load file string contains (can be from server)
var _s = file_to_string(_file);
//Load wrapper ds_map
//var _wrapper = ds_map_create();
//ds_map_read(_wrapper,_s);
var _wrapper = json_decode(_s);
//Load list "root" that was added to map.
var _list = ds_map_find_value(_wrapper,"ROOT");
//Load instances
for (var i=0; i < ds_list_size(_list); i+=1)
{
var _map = ds_list_find_value(_list, i);
var _obj = ds_map_find_value(_map, "obj");
with (instance_create_depth(0,0,0,asset_get_index(_obj)))
{
x = ds_map_find_value(_map, "x");
y = ds_map_find_value(_map, "y");
}
}
ds_map_destroy(_wrapper);
}
The code above can only save one object and does not save other global variables.
After saved:
Currently: (json_encode)
How I want: (ds_map_write)
I had made another similar code but it was very unstable.
As you can see, I saved it as json, but I just want to use data-structure (ds_map) and save everything in just one string, but it doesn't work. Can anyone help me to implement this code (save many objects and others global variables)?
(I have a lot of experience with GameMaker, since 2012, but this is my first time trying to save everything in a ds_map, I already read the manual but I still can't do it in a practical way. I already looked for several related topics but I can't find it!)
Last edited: