1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    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:
    76
    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,708
    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,708
    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:
    3,960
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice