P
Political Gangster
Guest
I just followed a tutorial by Shaun Spalding and it was kind of confusing for me to understand and transition my game from an ini saving system to JSON. If anyone can help me figure out what I'm doing wrong with my code that would be a huge help. What I would like to happen is that the game saves your character's position in a room when you enter or press s. Then when you press L I would like to create a new instance of the player character and move to the room they were in. Heres a link to the video
Heres my code.
Key Press -S
//Creat root list fo json
var _root_list = ds_list_create();
//for every instance, create a map
with (obj_player)
{
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, "y" , y);
ds_map_add (_map, "x" , x);
}
//Wrap the root list in a 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);
SaveStringToFile("savedgame.sav", _string);
//Destroy the data
ds_map_destroy(_wrapper);
show_debug_message("Game SAVE");
Key Press L
/// @description Insert description here
// You can write your code in this editor
with (obj_player) instance_destroy();
if (file_exists("savegame.sav"))
{
var _wrapper = LoadJSONFromFile("savedgame.sav");
var _list = _wrapper[?"ROOT"];
for (var i = 0; i < ds_list_size(_list); i++)
{
var _map= _list[| i];
var _obj = _map[? "obj"];
with (instance_create_layer(0,0,layer,asset_get_index(_obj)))
{
x = _map[? "X"];
y = _map[? "y"];
}
}
}
Heres my code.
Key Press -S
//Creat root list fo json
var _root_list = ds_list_create();
//for every instance, create a map
with (obj_player)
{
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, "y" , y);
ds_map_add (_map, "x" , x);
}
//Wrap the root list in a 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);
SaveStringToFile("savedgame.sav", _string);
//Destroy the data
ds_map_destroy(_wrapper);
show_debug_message("Game SAVE");
Key Press L
/// @description Insert description here
// You can write your code in this editor
with (obj_player) instance_destroy();
if (file_exists("savegame.sav"))
{
var _wrapper = LoadJSONFromFile("savedgame.sav");
var _list = _wrapper[?"ROOT"];
for (var i = 0; i < ds_list_size(_list); i++)
{
var _map= _list[| i];
var _obj = _map[? "obj"];
with (instance_create_layer(0,0,layer,asset_get_index(_obj)))
{
x = _map[? "X"];
y = _map[? "y"];
}
}
}