GML [SOLVED] Saving and loading a DS_List using a DS_Map?

Dr_Nomz

Member
Normally when I want to save and load something, I would take, for example, an enemy's local variables and save them using a controller object in the room end event. I can do the same with global variables.

I would use an enumerator with a ds_grid to save all the data to a string to be stored in a ds_map, and loaded back in the same way.

But I can only do that for local/global variables, ds_lists don't seem to work the same way. So how do I do it?

Here's my code for reference:
Code:
//obj_Control, Room End event (saving)
///Save Inventory Slots
if global.load_room=0{
enum save_inventory
{
  kind,
  count,
  column_count
}

var inventory_save = ds_grid_create(save_inventory.column_count,0);
for (var i=0;i<maxItems;i+=1){
  var _row = scr_DSGAR(inventory_save);
  inventory_save[# save_inventory.kind, _row]=global.inventory[i];
  inventory_save[# save_inventory.count, _row]=global.inventory_count[i];
}

var _key = "inventory_save_string";
scr_SDSV(_key,ds_grid_write(inventory_save));

ds_grid_destroy(inventory_save);
}
Code:
//Loading the game with a script from a file (ds_map_secure_load)

  var _key = "inventory_save_string"; //start Inventory Load
  var inventory_save = ds_grid_create(0,0);
  var _str = scr_SDGV(_key);

  ds_grid_read(inventory_save,_str);
  var i = 0;
  for (var _row=0;_row<ds_grid_height(inventory_save);_row++){
    global.inventory[i]=inventory_save[# save_inventory.kind, _row];
    global.inventory_count[i]=inventory_save[# save_inventory.count, _row];
    i++;
  }
 
  ds_grid_destroy(inventory_save); //end Inventory Load
Code:
///scr_DSGAR(grid index)
//scr_ds_grid_add_row
//Adds a row to the grid and returns the index of the new row
var _grid = argument[0];
ds_grid_resize(_grid,ds_grid_width(_grid),ds_grid_height(_grid)+1);
return(ds_grid_height(_grid)-1);
Code:
///scr_SDSV(key,value)
//scr_save_data_set_value
with(obj_Control)
  ds_map_replace(save_data,argument[0],argument[1]);
Code:
///scr_SDGV(key)
//scr_save_data_get_value
with(obj_Control)
  return save_data[? argument[0]];
 
D

Danei

Guest
You can ds_list_write() a ds_list to a string, and put that string in your ds_map.
To convert it back into a ds_list, simply ds_list_read() the string.

Code:
var testMap = ds_map_create();
var testList = ds_list_create(); //create structures

ds_list_add(testList, "blah", "etc"); //populate list

var testListString = ds_list_write(testList);  //convert the list to a string

testMap[? "savedList"] = testListString;  //save the string containing the list info to the map

var loadList = ds_list_create();  //create a new list to load into

ds_list_read(loadList, testMap[? "savedList"]); //read the string from the map back into a list.
 

Dr_Nomz

Member
I think it worked! For the record though, this is how I did it with my system:
Save: (room end)
Code:
var testListString = ds_list_write(option);  //convert the list to a string
scr_SDSV("savedList",testListString);
Load: (ds_map_secure_load)
Code:
ds_list_read(option, save_data[? "savedList"]);
It was a LOT simpler, and I didn't have to work around over complicating my code with trying to only use what you posted.

But, I worked with what you gave me and and it works, so thank you! :D
 
D

Danei

Guest
Yeah, I mean what I wrote was just an example of some code that put a list in a map and took it back out, not something I wanted you to use in your project verbatim. It's always better and more efficient to apply the principle to your own code base, so you did exactly the correct thing. Glad you got it working!
 
Top