SOLVED Trouble Saving and Loading an Array using ds_map/list

Gizzmo

Member
I'm just getting into understanding how to save and load my game and have had success with saving and loading single variables using the ds_map system, however I'm having many issues with doing the same with arrays.
I've tried using ds_lists and searched around the internet for anyone else asking the same thing.
From that I've gotten some parts of the code done but I'm not sure if the syntax is right or I'm missing something completely.

I'm trying to save an array that holds all the values for the players stats and attributes.

obj_manager create:
Code:
//Save file
save_data = ds_map_create();

file_name = "SaveData.sav";
obj_manager user_event(0): //Saving
Code:
//Save checkpoint
ds_map_replace(save_data,"current_area",current_area);
ds_map_replace(save_data,"checkpointx",global.checkpointx);
ds_map_replace(save_data,"checkpointy",global.checkpointy);

//Save stats
var list_stats = ds_list_create();
for (i = 0; i < 6; i++)
    ds_list_add(list_stats,obj_menu_smithing.stats[i]) //obj_menu_smithing holds the array
ds_map_add_list(save_data,"list_stats",list_stats)

ds_map_secure_save(save_data,file_name);
obj_manager user_event(1): //Loading
Code:
if !file_exists(file_name)
    exit;
    
ds_map_destroy(save_data);
save_data = ds_map_secure_load(file_name);

//Load checkpoint
current_area = ds_map_find_value(save_data,"current_area");
global.checkpointx = ds_map_find_value(save_data,"checkpointx");
global.checkpointy = ds_map_find_value(save_data,"checkpointy");

//Load stats
obj_menu_smithing.stats = ds_map_find_value(save_data,"list_stats");
 

chamaeleon

Member
If everything else is working, you can't expect
GML:
obj_menu_smithing.stats = ds_map_find_value(save_data,"list_stats");
to return an array. It will be a ds_list, which is not the same data type you used when you created the save map. You'll want to instead iterate over the ds_list content and assign it to an array.
GML:
var lst = save_data[? "list_stats"];
for (var i = 0; i < ds_list_size(lst); i++) {
    obj_menu_smithing.stats[i] = lst[| i];
}
 
Top