#Saving Object Information Created by External Object

1 messages · Page 1 of 1 (latest)

topaz ocean
#

Hi all! I'm working on a save and load system for my game that allows you to select an object in a turn-based combat system, then transfers the player to another room in order to engage in action combat, that return to the previous room and resume combat. The only problem is the enemies aren't loading back in, since they're created by an external object (obj_clockwork colliding with obj_player)

Does anyone know how to solve this? Thank you!


############################################################################################
ERROR in action number 1
of Create Event for object obj_battle_controller:
Variable <unknown_object>.enemies(100029, -2147483648) not set before reading it.
at gml_Object_obj_battle_controller_Create_0 (line 49) - for (var i = 0; i < array_length(enemies); i++)
############################################################################################
gml_Object_obj_battle_controller_Create_0 (line 49)
gml_Script_load_room (line 116)
gml_Object_obj_game_Step_0 (line 4) - load_room();

swift frost
#

So, I solved a vaguely analogous problem in that I was making a farming simulator and needed a way to save and load changes the player had made to the room, such as when tilling soil or planting crops.

The method I chose was to have an 'obj_persistence_manager' object, a persistent object that calls save/load functions on RoomStart and RoomEnd. When you leave a room loops through and puts anything about the current room that needs to persist into a struct, and then that struct gets baked into the saveData struct, which is what gets turned into a file if the player saves the game.

When the room starts, it loads that information and creates any needed instances.

#
function RoomStateSave(){
    var croparray = [];
    var numcrops = instance_number(oCrop);
    var instance;
    var roomname = room_get_name(room);
    var roomstruct = {};

    

    for (var i = 0; i < numcrops; i++){
        instance =  instance_find(oCrop,i);
        croparray[i]={};
        croparray[i].struct = instance.cropstruct;
        croparray[i].stage = instance.stage;
        croparray[i].X = instance.x;
        croparray[i].Y = instance.y;
        croparray[i].dayplanted = instance.dayplanted;
    }
    
    roomstruct.crops = croparray;
    

    var tilledsoil_layerID = layer_get_id("tilledsoil");
    var tilledsoil_mapID = layer_tilemap_get_id(tilledsoil_layerID);
    var _x;
    var _y;
    var _tiledata;
    var _tilearray = [];
    
    for (var i=0; i < room_width; i+=TILE_SIZE)
    {
    
        for (var ii = 0; ii < room_height; ii+= TILE_SIZE){
            _x = tilemap_get_cell_x_at_pixel(tilledsoil_mapID,i,ii)
            _y = tilemap_get_cell_y_at_pixel(tilledsoil_mapID,i,ii)
            _tiledata = tilemap_get(tilledsoil_mapID,_x,_y);
            _tilearray[i][ii] = _tiledata;
        }
    }
    
    roomstruct.tilledsoilarray = _tilearray;
    
    db_write(global.saveData,roomstruct,roomname)
}
#
function RoomStateLoad(){
var roomname = room_get_name(room);
roomstruct = db_read(global.saveData,-1,roomname);
if (roomstruct == -1) return;
var croparraylength = array_length(roomstruct.crops);

    for (var i=0; i < croparraylength; i++){
        with(instance_create_layer(roomstruct.crops[i].X, roomstruct.crops[i].Y,"Instances",oCrop)){
            cropstruct = other.roomstruct.crops[i].struct;
            stage = other.roomstruct.crops[i].stage;
            dayplanted = other.roomstruct.crops[i].dayplanted;
            sprite_index = other.roomstruct.crops[i].struct.sprite;
        }
    }

var tilledsoil_layerID = layer_get_id("tilledsoil");
var tilledsoil_mapID = layer_tilemap_get_id(tilledsoil_layerID);    
var _x; var _y;

    for (var i = 0; i < room_width; i += TILE_SIZE)
    {
        for (var ii = 0; ii < room_height; ii += TILE_SIZE){
            _x = tilemap_get_cell_x_at_pixel(tilledsoil_mapID,i,ii);
            _y = tilemap_get_cell_y_at_pixel(tilledsoil_mapID,i,ii);
            tilemap_set(tilledsoil_mapID,roomstruct.tilledsoilarray[i][ii],_x,_y);
        }
    }
}
#

The issue you are facing with needing to re-add the enemies is pretty much the same thing I do with the crop object.