Philip Wallin
Member
I'm having issues with the save & load system in Shaun Spalding's RPG series...
Shauns video.
Code:
Save Script:
Load Script:
Problem: I have an inventory, which uses arrays. global.Inventory = [ obj_apple, -1, -1], something like that. But when the inventory value is saved, then loaded, Inventory = 0, it's not an array anymore, just a singel number.
Any help would be appreciated.
With love, Philip.
Shauns video.
Code:
Save Script:
GML:
function save_game()
{
var _map = ds_map_create();
//Inventory
_map[? "Inventory"] = global.Inventory;
_map[? "bombs"] = global.bombs;
_map[? "arrows"] = global.arrows;
//Save All Intfo in a string
var _string = json_encode(_map);
save_string_to_file("save" + ".sav",_string);
show_debug_message(_string);
//Nuke the data
ds_map_destroy(_map);
show_debug_message("Game saved!");
}
function save_string_to_file(_filename,_string)
{
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 load_game()
{
var _file = "save" + ".sav";
if (file_exists(_file))
{
//Load game data
var _json = load_json_from_file(_file);
//Inventory
global.Inventory = _json[? "Inventory"];
global.arrows = _json[? "arrows"];
global.bombs = _json[? "bombs"];
return true;
}
else
{
return false;
}
}
function load_json_from_file(_filename)
{
var _buffer = buffer_load(_filename);
var _string = buffer_read(_buffer, buffer_string);
buffer_delete(_buffer);
var _json = json_decode(_string);
return _json;
}
Any help would be appreciated.
With love, Philip.