1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 Can anyone Help me if there is something wrong to my script ^-^

Discussion in 'Programming' started by Sugor, Jun 4, 2019.

  1. Sugor

    Sugor Member

    Joined:
    May 28, 2019
    Posts:
    17
    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...
     
  2. Azenris

    Azenris Member

    Joined:
    Oct 30, 2016
    Posts:
    111
    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()
     
  3. Sugor

    Sugor Member

    Joined:
    May 28, 2019
    Posts:
    17
    Code:
            var prev_room = save_data[? "room"];
    
            if (room != prev_room) {
                room_goto(prev_room);
            }
     
    this isn't gonna work at all?
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,872
    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.
     
    Yal likes this.
  5. Sugor

    Sugor Member

    Joined:
    May 28, 2019
    Posts:
    17
    Dang! ireally dont know where to put it i'm actually a beginner its from a tutorial... Sorry ^^... i hope u can help me
     
  6. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,872
    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.
     
    Yal and EvanSki like this.
  7. Sugor

    Sugor Member

    Joined:
    May 28, 2019
    Posts:
    17
    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: Jun 5, 2019
  8. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    4,214
    If you have instance-local variables to save, not just global ones, make sure you read them back to the right object.
     
  9. Sugor

    Sugor Member

    Joined:
    May 28, 2019
    Posts:
    17
    I just type [? "expr"] wrong... first its epr only hehe
     

Share This Page