W
Woochi
Guest
Hi, i have been trying to save and load nested ds_maps, partially without succes. I need nested maps for saving player data, as i save the data of all players in one single file. So the highest map of the file contains the account names, where each is a map on its own, containing data like password, level, name, but also containing another map representing the inventory of that player.
How i have been doing it:
saving:
1. have ds_map A and ds_map B.
2. add map B to map A under key K, using ds_map_add_map.
3. save map A, using ds_map_secure_save("filename.json").
loading:
1. load map A from the previously created file into variable X, using ds_map_secure_load("filename.json").
2. assign map B to variable Y, using ds_map_find_value(A,K).
then typically, when i check with ds_exists wether Y exists as a map, it returns false. But when check X, it returns true.
exact code (creating account/character):
loading players data (at login):
(the file is already created in a prior check, containing an empty ds_map)
then the player object is created, which runs the following code:
(ds_exists(inv,ds_type_map) returns false, other data like race, class, lvl etc seems to load fine)
Saving playerdata:
(error at ds_map_replace_map)
Does anyone know what im doing wrong? Also, a clean example of creating, saving and loading nested ds maps would be appreciated a lot!
How i have been doing it:
saving:
1. have ds_map A and ds_map B.
2. add map B to map A under key K, using ds_map_add_map.
3. save map A, using ds_map_secure_save("filename.json").
loading:
1. load map A from the previously created file into variable X, using ds_map_secure_load("filename.json").
2. assign map B to variable Y, using ds_map_find_value(A,K).
then typically, when i check with ds_exists wether Y exists as a map, it returns false. But when check X, it returns true.
exact code (creating account/character):
Code:
with (obj_textbox) {
if (self.name == "reg_acc") {
var acc = self.txt;
}
}
with (obj_textbox_crypt) {
if (self.name == "reg_pass") {
var pass = self.txt;
}
}
with (obj_textbox) {
if (self.name == "reg_name") {
var name = self.txt;
}
}
var create_data_all = ds_map_secure_load("playerdata.json");
if (ds_map_exists(create_data_all,acc)) {
scr_sysmsg("Account already exists.","reg",c_white);
} else {
if (string_length(acc)<1) {
scr_sysmsg("Enter account name.","reg",c_white);
} else if (string_length(pass)<1) {
scr_sysmsg("No password given.","reg",c_white);
} else if (obj_controller_reg.pick == "") {
scr_sysmsg("Decide the character's race.","reg",c_white);
} else if (name == "") {
scr_sysmsg("Name the character","reg",c_white);
} else {
var create_data_player = ds_map_create();
ds_map_add(create_data_player,"user",acc);
ds_map_add(create_data_player,"name",name);
ds_map_add(create_data_player,"password",pass);
ds_map_add(create_data_player,"race",obj_controller_reg.pick);
ds_map_add(create_data_player,"lvl",1);
ds_map_add(create_data_player,"Exp",0);
ds_map_add(create_data_player,"class","");
ds_map_add(create_data_player,"loc","");
var create_inv = ds_map_create();
ds_map_add(create_inv,"potion_hp",2);
ds_map_add_map(create_data_player,"inv",create_inv);
ds_map_add_map(create_data_all,acc,create_data_player);
ds_map_secure_save(create_data_all,"playerdata.json");
ds_map_destroy(create_data_player);
ds_map_destroy(create_inv);
scr_hyper("reg_back");
scr_sysmsg("Account created succesfully!","",c_black);
}
}
ds_map_destroy(create_data_all);
Code:
var acc;
var pass;
with (obj_textbox) {
if (self.name == "user_acc") {
acc = self.txt;
}
}
with (obj_textbox_crypt) {
if (self.name == "user_pass") {
pass = self.txt;
}
}
var login_data_all = ds_map_secure_load("playerdata.json");
var login_data_player = ds_map_find_value(login_data_all,acc);
if (ds_map_exists(login_data_all,acc)) {
if (pass == login_data_player[? "password"]) {
global.playerdata = ds_map_create();
ds_map_copy(global.playerdata,login_data_player);
ds_map_destroy(login_data_all);
ds_map_destroy(login_data_player);
instance_create(0,0,obj_controller_postlogin);
with (all) {
if (self.group == "login") {
instance_destroy();
}
}
} else {
scr_sysmsg("Password is incorrect.","login",c_black);
}
} else {
scr_sysmsg(string("Account ("+acc+") does not exist."),"login",c_black);
}
then the player object is created, which runs the following code:
Code:
name = ds_map_find_value(global.playerdata,"name");
race = ds_map_find_value(global.playerdata,"race");
class = ds_map_find_value(global.playerdata,"class");
lvl = ds_map_find_value(global.playerdata,"lvl");
Exp = ds_map_find_value(global.playerdata,"Exp");
loc = ds_map_find_value(global.playerdata,"loc");
inv = global.playerdata[? "inv"];
Saving playerdata:
Code:
var user = ds_map_find_value(global.playerdata,"user");
var data_all = ds_map_secure_load("playerdata.json");
var data_self = ds_map_find_value(data_all,user);
ds_map_replace_map(data_all,user,global.playerdata);
ds_map_secure_save(data_all,"playerdata.json");
ds_map_destroy(data_all);
scr_sysmsg("Saved succesfully","",c_black);
Does anyone know what im doing wrong? Also, a clean example of creating, saving and loading nested ds maps would be appreciated a lot!