Legacy GM Can anyone Help me if there is something wrong to my script ^-^

Sugor

Member
Scr_SaveGame
Code:
///scr_savegame()

// Make sure the player exist
if (!instance_exists(obj_player_stats)) exit;

// Create a save data structure
var save_data = ds_map_create();

with (obj_player_stats){
    save_data[? "room"] = prev_room;
    save_data[? "x"] = player_xstart;
    save_data[? "y"] = player_ystart;
    save_data[? "hp"] = hp;
    save_data[? "maxhp"] = maxhp;
    save_data[? "stamina"] = stamina;
    save_data[? "maxstamina"] = maxstamina;
    save_data[? "expr"] = expr;
    save_data[? "maxepr"] = maxexpr;
    save_data[? "level"] = level;
    save_data[? "attack"] = attack;
    
}

var save_string = json_encode(save_data);
ds_map_destroy(save_data);

var file = file_text_open_write(working_directory + "mysavegame.txt");
file_text_write_string(file, save_string);
file_text_close(file);

show_message("SAVED");
And the scr_load_game()
Code:
///scr_load_game
var file = file_text_open_read(working_directory + "mysavegame.txt");
var save_string = file_text_read_string(file);
        file_text_close(file);
        save_string = base64_decode(save_string);
        var save_data = json_decode(save_string);
      
        var prev_room = save_data[? "room"]; 
        if (room != prev_room) {
            room_goto(prev_room);
        }
        
      with (obj_player_stats) {
      player_xstart = save_data[? "x"];
      player_ystart = save_data[? "y"];
      if (instance_exists(obj_player)) {
               obj_player.x = player_xstart;
               obj_player.y = player_ystart;
               obj_player.phy_position_x = player_xstart;
               obj_player.phy_position_y = player_ystart;
           }else{
                instance_create(player_xstart, player_ystart, obj_player);
           }
           hp = save_data[? "hp"];
           maxhp = save_data[? "maxhp"];
           stamina = save_data[? "stamina"];
           maxstamina = save_data[? "maxstamina"];
           expr = save_data[? "expr"];
           maxexpr = save_data[? "maxexpr"];
           level = save_data[? "level"];
           attack = save_data[? "attack"];
        }
ds_map_destroy(save_data);

The error is that its says that the room_goto is suppose to be a number...
 

Azenris

Member
is prev_room a number when you save it ?


room_goto requires an ID, not a string name.

If you saved prev_room using room_get_name

then in your load you need to turn it back into an asset id using asset_get_index()
 

Sugor

Member
Code:
        var prev_room = save_data[? "room"];

        if (room != prev_room) {
            room_goto(prev_room);
        }
this isn't gonna work at all?
 

FrostyCat

Member
Code:
        var prev_room = save_data[? "room"];

        if (room != prev_room) {
            room_goto(prev_room);
        }
this isn't gonna work at all?
The part you described will work if the room ordering has not changed, because of this in the saving code:
Code:
save_data[? "room"] = prev_room;
If you intend to reorder the rooms and have older save files still work, you should save room_get_name(prev_room); and read it back with var prev_room = asset_get_index(save_data[? "room"]);.

But that's a side note. The real reason your code doesn't work is because you put a base64 decode on the loading code, but missing the base64 encode on the saving code.
 
  • Like
Reactions: Yal

Sugor

Member
The part you described will work if the room ordering has not changed, because of this in the saving code:
Code:
save_data[? "room"] = prev_room;
If you intend to reorder the rooms and have older save files still work, you should save room_get_name(prev_room); and read it back with var prev_room = asset_get_index(save_data[? "room"]);.

But that's a side note. The real reason your code doesn't work is because you put a base64 decode on the loading code, but missing the base64 encode on the saving code.
Dang! ireally dont know where to put it i'm actually a beginner its from a tutorial... Sorry ^^... i hope u can help me
 

FrostyCat

Member
Dang! ireally dont know where to put it i'm actually a beginner its from a tutorial... Sorry ^^... i hope u can help me
There's really only one place to put it if you gave any thought to what I said. It's even in the Manual.
Code:
file_text_write_string(file, base64_encode(save_string));
Go over that tutorial again and re-study the part on saving. If the loading part contains a decode, the saving part must contain an encode. It's common sense.
 

Sugor

Member
There's really only one place to put it if you gave any thought to what I said. It's even in the Manual.
Code:
file_text_write_string(file, base64_encode(save_string));
Go over that tutorial again and re-study the part on saving. If the loading part contains a decode, the saving part must contain an encode. It's common sense.
Thank you for helping now i know where i did wrong.:):banana:
Edit: My Problem now is the i made a Draw GUI event but i don't know how to add them on save_scripts
i tried the
save_data[? "healthbar"] = healthbar;
then on the
load_scripts
healthbar = save_data[? "healthbar"];
i didn't work
i have it on the obj_player_stats healthbar = spr_healthbar;
 
Last edited:

Yal

šŸ§ *penguin noises*
GMC Elder
Thank you for helping now i know where i did wrong.:):banana:
Edit: My Problem now is the i made a Draw GUI event but i don't know how to add them on save_scripts
i tried the
save_data[? "healthbar"] = healthbar;
then on the
load_scripts
healthbar = save_data[? "healthbar"];
i didn't work
i have it on the obj_player_stats healthbar = spr_healthbar;
If you have instance-local variables to save, not just global ones, make sure you read them back to the right object.
 
Top