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

Doober

Member
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.
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
Please post what you've tried so far. This will allow us to pinpoint where the problem lies.
 

Doober

Member
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);
-------------------------------------------------------------------------------------------------------------
 

FrostyCat

Member
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.
 

Doober

Member
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
 
Top