LinkDudeWolf
Member
So I have been trying to save the x and y coordinates of the player in the room and load the player in the same x and y coordinates (the same place he was when the game is saved but I can't get it to work can anyone help? I thought using the .x and .y after an object meant saving the location of that object so why doesn't it work?? Object1 is the player by the way.
Saving object code :
Loading object code :
Saving object code :
GML:
var _saveData = array_create(0);
//for every instance, create a struct and add it to the array
with (Object1)
{
var _saveEntity =
{
obj : object_get_name(object_index),
saved_room : room,
save_name : global.realName,
player_x : Object1.x,
player_y : Object1.y,
}
array_push(_saveData, _saveEntity);
}
//turn all this data into a JSON string and save it via a buffer
var _string = json_stringify(_saveData);
var _buffer = buffer_create(string_byte_length(_string) +1, buffer_fixed, 1);
buffer_write( _buffer, buffer_string, _string);
buffer_save ( _buffer, "savedgame.save");
buffer_delete( _buffer);
Loading object code :
GML:
if (file_exists("savedgame.save"))
{
var _buffer = buffer_load("savedgame.save");
var _string = buffer_read( _buffer, buffer_string);
buffer_delete( _buffer);
var _loadData = json_parse( _string);
while (array_length(_loadData) > 0)
{
var _loadEntity = array_pop(_loadData);
with (instance_create_layer(0,0,layer,asset_get_index(_loadEntity.obj)))
{
room = _loadEntity.saved_room;
Object1.x = _loadEntity.player_x;
Object1.y = _loadEntity.player_y;
global.realName = _loadEntity.save_name;
}
}
}
Last edited: