M
MissCheeseDanish
Guest
Hey, so I'm working on a game demo for college, and for the past few weeks my adviser and I have been stuck on the save/load system. Right now it's getting close to where it should be, but there's still the problem that with every load, a new player object is created.
Save:
Load:
In the debugger, I think we found that in the load script, it skips over checking if global.player (which is set to one of the player objects, because there's a character select) exists, and goes straight to the else, where another player object is created.
Any help is appreciated!
Save:
Code:
/// scr_save_game
// Make sure the player exists
if (global.player == noone) {
exit;
}
// Create the save data structure
var save_data = ds_map_create();
var save_player = global.player;
ds_map_add(save_data, "player", save_player);
with (obj_input) {
ds_map_add(save_data, "room", previous_room);
ds_map_add(save_data, "x", player_xstart);
ds_map_add(save_data, "y", player_ystart);
}
with (global.player) {
ds_map_add(save_data, "hp", hp);
ds_map_add(save_data, "maxhp", maxhp);
ds_map_add(save_data, "stamina", stamina);
ds_map_add(save_data, "maxstamina", maxstamina);
ds_map_add(save_data, "expr", expr);
ds_map_add(save_data, "maxexpr", maxexpr);
ds_map_add(save_data, "level", level);
ds_map_add(save_data, "attack", attack);
ds_map_add(save_data, "defense", defense);
ds_map_add(save_data, "m_attack", m_attack);
ds_map_add(save_data, "m_defense", m_defense);
ds_map_add(save_data, "tired", tired);
ds_map_add(save_data, "dead", dead);
}
with (obj_input) {
ds_map_add(save_data, "day", global.day);
ds_map_add(save_data, "flans", global.flans);
}
var save_string = json_encode(save_data);
ds_map_destroy(save_data);
//save_string = base64_encode(save_string);
var file = file_text_open_write(working_directory + "PROXYsave.txt");
file_text_write_string(file, save_string);
file_text_close(file);
Code:
/// scr_load_game
var file = file_text_open_read(working_directory + "PROXYsave.txt");
var save_string = "";
while (!file_text_eof(file)) {
save_string += file_text_readln(file);
//file_text_readln(file);
}
file_text_close(file);
//save_string = base64_decode(save_string);
var save_data = json_decode(save_string);
var save_room = ds_map_find_value(save_data, "room");
if (room != save_room) {
room_goto(save_room);
if (instance_number(global.player) > 1) {
with (global.player) {
instance_destroy();
}
}
}
var load_player = ds_map_find_value(save_data, "player");
if (load_player != global.player) {
with (global.player) {
instance_change(load_player, true);
}
}
with (obj_input) {
player_xstart = ds_map_find_value(save_data, "x");
player_ystart = ds_map_find_value(save_data, "y");
if (instance_exists(global.player)) {
global.player.x = player_xstart;
global.player.y = player_ystart;
global.player.phy_position_x = player_xstart;
global.player.phy_position_y = player_ystart;
} else {
if (instance_number(global.player) > 1) {
instance_destroy();
}
instance_create(obj_input.player_xstart, obj_input.player_ystart, global.player);
}
}
with (global.player) {
hp = ds_map_find_value(save_data, "hp");
maxhp = ds_map_find_value(save_data, "maxhp");
stamina = ds_map_find_value(save_data, "stamina");
maxstamina = ds_map_find_value(save_data, "maxstamina");
expr = ds_map_find_value(save_data, "expr");
maxexpr = ds_map_find_value(save_data, "maxexpr");
level = ds_map_find_value(save_data, "level");
attack = ds_map_find_value(save_data, "attack");
defense = ds_map_find_value(save_data, "defense");
m_attack = ds_map_find_value(save_data, "m_attack");
m_defense = ds_map_find_value(save_data, "m_defense");
tired = ds_map_find_value(save_data, "tired");
dead = ds_map_find_value(save_data, "dead");
}
with (obj_input) {
global.day = ds_map_find_value(save_data, "day");
global.flans = ds_map_find_value(save_data, "flans");
}
ds_map_destroy(save_data);
In the debugger, I think we found that in the load script, it skips over checking if global.player (which is set to one of the player objects, because there's a character select) exists, and goes straight to the else, where another player object is created.
Any help is appreciated!