F
Finn
Guest
I am working on a simple version of a depth system (mostly drawing objects in order of y coordinate).
I am using a grid to manage the objects, sort them and then draw them in the right order.
I was careful setting up the data structure and managing it but encountered some strange memory leak with it nevertheless that I would really like to understand. Any advise is appreciated!
Now the problem is with the step event at (!) above:
If I first resize the grid and then clear it; memory usage will keep increasing constantly every step. (This becomes more dramatic if I write some extra long strings into the grid for debugging.)
First resizing and then clearing sounds like a good choice and it is done the same way in the GML documendation for ds_grid_resize();
Solution (?) : If I first clear and then resize the grid, memory usage will stay stable and not increase every step. I dont know if that is actually stable or just seems like it is fixed because when clearing I just write a simple integer into the grid which takes less memory than the original complex and heterogeneous data.
PS: I destroy the grid in the clean_up event of the object. I am also additionally destroying it at every room_end and creating a new grid at every room_start for safety now :S
PPS: The script above that writes each instance into ds_depthgrid; I am grateful for any recommendations for performance improvement.
I am using a grid to manage the objects, sort them and then draw them in the right order.
I was careful setting up the data structure and managing it but encountered some strange memory leak with it nevertheless that I would really like to understand. Any advise is appreciated!
Code below. Roughly the simple depth system works like this:
Code: - Write all instance ids of relevant objects and their y-values into a grid
- Sort that grid by y-value
- Iterate through the sorted grid and execute the draw event of each object in the grid
Code:
// --------------------------------------------------------------------------------
// CREATE EVENT
// D S G R I D S T R U C T U R E
ds_depthgridW = 2; if (DEBUG_MODE_ENABLED) { ds_depthgridW = 3;} // number of columns
ds_depthgrid = ds_grid_create(ds_depthgridW, 1);
ds_depthgridH = 0; // number of rows
ds_depthgrid_pointer = 0; // pointer to last row that was populated
// --------------------------------------------------------------------------------
// STEP EVENT
var _grid = ds_depthgrid;
// Reset depth grid
ds_depthgrid_pointer = 0;
ds_depthgridH = 0;
// Get total number of instances to draw this step
ds_depthgridH = instance_number(obj_depthSystem)
if (ds_depthgridH <=0) {exit;}
// Setup grid: resize & clear (!)
ds_grid_resize(_grid, ds_depthgridW, ds_depthgridH);
ds_grid_clear(_grid, noone);
// Populate Grid
with(obj_depthSystem) { sc_registerInst_with_depthGrid(_grid); }
// --------------------------------------------------------------------------------
// SCRIPT sc_registerInst_with_depthGrid()
// R E G I S T E R I N S T A N C E W I T H D E P T H G R I D
var _grid = argument[0];
var _pointer = other.ds_depthgrid_pointer;
//var _grid = other.ds_depthgrid;
_grid[# 0, _pointer] = id; // instance ID
_grid[# 1, _pointer] = yDepth; // y-coordinate
if (DEBUG_MODE_ENABLED) {
_grid[# 2, _pointer] = object_get_name(object_index); // object name
}
// increment draw counter for next instance in depthgrid within obj_masterDraw
other.ds_depthgrid_pointer++;
// --------------------------------------------------------------------------------
// DRAW EVENT
// D R A W O B J E C T S I N O R D E R O F y D E P T H
// A C T I V A T E D R A W E V E N T S
var _grid = ds_depthgrid;
var _i = 0;
repeat(ds_depthgridH){
var _inst = _grid[# 0, _i];
with(_inst){
drawingActive = true;
event_perform(ev_draw, 0); // This will (exclusively) perform main draw event of respective object;
drawingActive = false;
}
_i++;
}
Now the problem is with the step event at (!) above:
If I first resize the grid and then clear it; memory usage will keep increasing constantly every step. (This becomes more dramatic if I write some extra long strings into the grid for debugging.)
First resizing and then clearing sounds like a good choice and it is done the same way in the GML documendation for ds_grid_resize();
Solution (?) : If I first clear and then resize the grid, memory usage will stay stable and not increase every step. I dont know if that is actually stable or just seems like it is fixed because when clearing I just write a simple integer into the grid which takes less memory than the original complex and heterogeneous data.
Code:
// "SOLUTION"?
// Setup grid: resize & clear
ds_grid_clear(_grid, noone);
ds_grid_resize(_grid, ds_depthgridW, ds_depthgridH);
PS: I destroy the grid in the clean_up event of the object. I am also additionally destroying it at every room_end and creating a new grid at every room_start for safety now :S
PPS: The script above that writes each instance into ds_depthgrid; I am grateful for any recommendations for performance improvement.