1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Windows How to make a save and load game function with json files?

Discussion in 'Programming' started by Doober, May 10, 2019.

  1. Doober

    Doober Member

    Joined:
    May 10, 2019
    Posts:
    3
    I have been working on my game for quite some time and I am working on trying to add a save and load game feature to my game. The problem is I can't ever get it to work no matter how many google searches I do or how many YouTube videos I watch. If anyone could help me to understand this so I can fix this problem, I would be eternally grateful for the help.
     
  2. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,253
    Please post what you've tried so far. This will allow us to pinpoint where the problem lies.
     
  3. Doober

    Doober Member

    Joined:
    May 10, 2019
    Posts:
    3
    This is how iv'e structured it
    -----------------------------------------------------------------------------------------------------------------------------------

    Save The File:

    /// Saved Game Files

    /// Does The Player Character Exist

    if (!instance_exists(obj_variables)) exit;

    /// Create The Save File

    var save_data = ds_map_create();

    save_data[?"room"] = global.previous_room;
    save_data[?"x"] = 0;
    save_data[?"y"] = 0;
    save_data[?"22"] = global.life;
    save_data[?"65"] = global.stamina;

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

    var file = file_text_open_write(working_directory + "SavedGameGFC1.txt");
    file_text_write_string(file, save_data);

    file_text_close(file);




    /// The Game File Was Saved
    show_message("Your Game Was Successfully Saved My Dude");
    --------------------------------------------------------------------------------------------------

    Load The Save File:



    /// Load Game Files

    var file = file_text_open_read(working_directory + "SavedGameGFC1.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 save_room = save_data[?"room"];
    if (room != save_room)
    {
    room_goto(room);
    }

    with (obj_variables)
    {
    player_xstart = save_data[? "x"];
    player_ystart = save_data[? "y"];
    if (instance_exists(obj_player_1))
    {
    obj_player_1.x = player_xstart;
    obj_player_1.y = player_ystart;
    obj_player_1.phy_position_x = player_xstart;
    obj_player_1.phy_position_y = player_ystart;
    }
    else
    {
    instance_create(player_xstart, player_ystart, obj_player_1);
    }

    global.life = save_data[? "22"];
    global.satmina = save_data[? "65"];
    }

    ds_map_destroy(save_data);
    -------------------------------------------------------------------------------------------------------------
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,962
    Your saving code doesn't have a base64 encode, yet your loading code expects base64 content and decodes first. This mismatch is a mess waiting to happen. Either add a base64 encode to the saving side, or remove the base64 decode from the loading side.

    This is the kind of problem that is much easier to solve by walking through the code yourself than searching on Google or YouTube, as are most problems with specific implementations of code. If you want to improve at programming, first stop shirking off the responsibility for your code to the Internet.
     
  5. Doober

    Doober Member

    Joined:
    May 10, 2019
    Posts:
    3
    I'm sorry, this is the first time i have tried more than basic coding and I kinda got lost. To tell you the truth this is the first time I EVER tried implementing a save system to my game so I didn't realize how hard it was going to be, but thank you for taking the time to help me with my problem and I will try to do better next time! :3
     

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