H
Haulidaie
Guest
Hi, first thread. I usually don't post to forums so go easy on me.
I'm using a ds_grid (whose data is written within a ds_map) to store positions of objects in my room. I've got a save script that looks like this:
And I've got a load script that looks like this:
So my actual problem is that my global.cellSize, and most objects are 16*16, but some objects (buildings) are 32*32. For all of the smaller objects, these scripts work fine. Everything is saved were it's meant to, and everything is spawned in the right place upon loading.
However, the 32*32 (or larger) objects always spawn one cell (16 pixels) further down the room than where they originally were.
Now I've tested, and messed around with it for hours, and the size of the object doesn't change how far down it moves. Also, I've become pretty sure that it's the load script, specifically, the instance creation itself that's the issue. But I just can't prove it.
What's even more confusing, is when I tried to just jump around the issue with this code:
It didn't work! The same thing happened!
Now I'm probably just being an idiot, or missed something stupidly small, but I am well and truly stumped. Any help would be greatly appreciated. I'm using the latest version of GMS 2.
I'm using a ds_grid (whose data is written within a ds_map) to store positions of objects in my room. I've got a save script that looks like this:
Code:
///@description Save the room's objects to a grid, and then store in ds_map
with(obj_room_manager) {
var
rName = room_get_name(room),
hCells = room_width div global.cellSize,
vCells = room_height div global.cellSize,
grid = ds_grid_create(hCells+1, vCells+1);
//Loop through every cell
for(var v = 0; v < vCells; v++) {
for(var h = 0; h < hCells; h++) {
//Get instance in cell (if there is one)
var inst = instance_position(h*global.cellSize, v*global.cellSize, obj_parent_solid);
if(inst != noone and grid[# h, v] != "0") {
//Handle large grid objects
if(inst.sprite_width > global.cellSize or inst.sprite_height > global.cellSize) {
ds_grid_set_region(grid, h, v, h+(inst.sprite_width/global.cellSize)-1, v+(inst.sprite_height/global.cellSize)-1, "0");
}
grid[# h, v] = inst.object_index;
} else {
grid[# h, v] = "0";
}
}
}
//Save grid, and tidy
global.sRooms[? rName] = ds_grid_write(grid);
ds_grid_destroy(grid);
show_message("Saved!");
}
Code:
///@description Load room objects from saved grid
with(obj_room_manager) {
var
rName = room_get_name(room),
hCells = room_width div global.cellSize,
vCells = room_width div global.cellSize,
grid = ds_grid_create(hCells+1, vCells+1);
//Check if the room has a saved grid
if(global.sRooms[? rName] = undefined) {
ds_grid_destroy(grid);
return(false);
} else {
ds_grid_read(grid, global.sRooms[? rName]);
}
//Loop through every cell
for(var v = 0; v < vCells; v++) {
for(var h = 0; h < hCells; h++) {
var inst = real(grid[# h, v]);
//Add saved grid data to room
if(inst > 0) {
instance_create_layer(h*global.cellSize, v*global.cellSize, "Instances", inst);
}
}
}
//Tidy
ds_grid_destroy(grid);
show_message("Loaded!");
return(true);
}
However, the 32*32 (or larger) objects always spawn one cell (16 pixels) further down the room than where they originally were.
Now I've tested, and messed around with it for hours, and the size of the object doesn't change how far down it moves. Also, I've become pretty sure that it's the load script, specifically, the instance creation itself that's the issue. But I just can't prove it.
What's even more confusing, is when I tried to just jump around the issue with this code:
Code:
//Load script instance creation replacement
if(inst.sprite_height > global.cellSize) {
instance_create_layer(h*global.cellSize, (v-1)*global.cellSize, "Instances", inst);
} else {
instance_create_layer(h*global.cellSize, v*global.cellSize, "Instances", inst);
}
Now I'm probably just being an idiot, or missed something stupidly small, but I am well and truly stumped. Any help would be greatly appreciated. I'm using the latest version of GMS 2.