• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How do I Save and Create my character and objects in a Room Using JSON

P

Political Gangster

Guest
I just followed a tutorial by Shaun Spalding and it was kind of confusing for me to understand and transition my game from an ini saving system to JSON. If anyone can help me figure out what I'm doing wrong with my code that would be a huge help. What I would like to happen is that the game saves your character's position in a room when you enter or press s. Then when you press L I would like to create a new instance of the player character and move to the room they were in. Heres a link to the video


Heres my code.

Key Press -S


//Creat root list fo json
var _root_list = ds_list_create();

//for every instance, create a map
with (obj_player)
{
var _map =ds_map_create();
ds_list_add(_root_list,_map);
ds_list_mark_as_map(_root_list,ds_list_size(_root_list)-1);

var _obj = object_get_name(object_index);
ds_map_add (_map, "obj", _obj);
ds_map_add (_map, "y" , y);
ds_map_add (_map, "x" , x);


}

//Wrap the root list in a map
var _wrapper = ds_map_create();
ds_map_add_list(_wrapper, "ROOT", _root_list);

//Save all of this to a string
var _string =json_encode(_wrapper);
SaveStringToFile("savedgame.sav", _string);

//Destroy the data
ds_map_destroy(_wrapper);

show_debug_message("Game SAVE");


Key Press L

/// @description Insert description here
// You can write your code in this editor

with (obj_player) instance_destroy();

if (file_exists("savegame.sav"))
{
var _wrapper = LoadJSONFromFile("savedgame.sav");
var _list = _wrapper[?"ROOT"];

for (var i = 0; i < ds_list_size(_list); i++)
{
var _map= _list[| i];

var _obj = _map[? "obj"];
with (instance_create_layer(0,0,layer,asset_get_index(_obj)))
{
x = _map[? "X"];
y = _map[? "y"];
}

}
}
 

FrostyCat

Redemption Seeker
JSON isn't so different from INI, except it allows more nesting layers than just 1 (i.e. instead of always section-key, you can have section-section-key and more) and the syntax isn't the same. Like INI, you must read the same format you write, and you can't read what you don't write.

Here's an obvious example from your code not reading the same format as you write (notice the mismatching case):
Code:
ds_map_add (_map, "x" , x);
Code:
x = _map[? "X"];
And nowhere in your code is the room ever written, so you can't expect to load that.

I would suggest that you redo the format, as you only have one player (so the list and the explicit object type are both unnecessary) and you need the room (which requires an entry of its own). Here's what it may look like:
Code:
{
  "player_x": 234,
  "player_y": 532,
  "player_room": "rm_level2"
}
Then you can simply write like this:
Code:
var topmap = ds_map_create();
topmap[? "player_x"] = obj_player.x;
topmap[? "player_y"] = obj_player.y;
topmap[? "player_room"] = room_get_name(room);
SaveStringToFile("savedgame.sav", json_encode(topmap));
ds_map_destroy(topmap);
And load like this (must from a persistent source as this requires travelling across rooms, and it can't be obj_player):
Code:
topmap = LoadJSONFromFile("savedgame.sav");
room_goto(asset_get_index(topmap[? "player_room"]));
alarm[0] = 1;
Code:
// Alarm 0
instance_create_layer(topmap[? "player_x"], topmap[? "player_y"], "Instances", obj_player);
 

PlayerOne

Member
I suggest you watch this. The method Shaun used is more like a quick save than an actual save function. I recomend this video:

 
P

Political Gangster

Guest
If I wanted to save other objects and characters down the line should I still get ride of the list?

Thanks for the help, I replaced the code I had with Frosty's and it works. If you could share any more resources on how this works I would appreciate it because in the future I'd like to save the characters items and choices as well. I've had a difficult time finding many tutorials for gms2 online. Additionally, the video I initially watched included some scripts I didn't include here but I don't think I need them anymore.

I'm also now having an issue where if I want to load back to something that happened in the same room it creates an additional character instead of moving my persistent one.

***************
Moderator Note: Three back-to-back posts merged. Please don't bump your own topic unless a day or so has passed without a response.
 
Last edited by a moderator:
Top