GMS 2 Deactivating Objects With Code

Rexzqy

Member
Hello everybody,

I am trying to deactivate objects for better performance. For objects without code it went pretty smoothly:

(this is in an alarm that fires every 30 steps)
Code:
for(var i = 0; i < array_length(deactivate_nocode_list); i++)
{
    instance_deactivate_object(deactivate_nocode_list[i]);
}

instance_activate_region(obj_player.x-550,obj_player.y-300,obj_player.x+500,obj_player.y+300,true);
However, I am not sure how or even if I should deactivate those with codes. Should I deactivate them by:

GML:
if point_in_rectangle(x,y,obj_player.x-550,obj_player.y-300,obj_player.x+550,obj_player.y+300) = false
{
    instance_deactivate_object(id);
}
and then record the ID and the deactivated time of it in a grid? Will that help the performance or worsen it? Any help or advice is greatly appreaciated!

EDIT: examples of objects with code includes seeds, which will grow into plants after a while. When I activate it again, I will add the amount of time since deactivation to the timer for growth. I am just wondering if this will actually help the performance or worsen it since it takes a grid and an addition alarm for every objects that need to be deactivated.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
It's very difficult to say whether it will help or not as the only honest answer is... it depends!!! Deactivation is quite a slow process, but it has the benefit of only working on a few instances at a time after the initial culling is done when you start a room. I would advise creating a "stress test" room, where you create a worst case scenario for the game and then do some benchmarking with and without deactivation. You can then test both of them using the GM debugger and the profiler and what kind of performance you get and see how things like moving the camera around affect the performance.
 

Rexzqy

Member
It's very difficult to say whether it will help or not as the only honest answer is... it depends!!! Deactivation is quite a slow process, but it has the benefit of only working on a few instances at a time after the initial culling is done when you start a room. I would advise creating a "stress test" room, where you create a worst case scenario for the game and then do some benchmarking with and without deactivation. You can then test both of them using the GM debugger and the profiler and what kind of performance you get and see how things like moving the camera around affect the performance.
Thx, Got it! Will test it tmrw.
 

Rexzqy

Member
It's very difficult to say whether it will help or not as the only honest answer is... it depends!!! Deactivation is quite a slow process, but it has the benefit of only working on a few instances at a time after the initial culling is done when you start a room. I would advise creating a "stress test" room, where you create a worst case scenario for the game and then do some benchmarking with and without deactivation. You can then test both of them using the GM debugger and the profiler and what kind of performance you get and see how things like moving the camera around affect the performance.
It actually helped a lot(~500 instances, +80 fps)! Below is how I record the instance & the deactivation time into a grid just in case anyone needs it in the future.

GML:
for(var i = 0; i < ds_grid_height(global.ds_deactivate); i++)
{
    if global.ds_deactivate[# 0,i] = id //check if this instance is in the grid
    {
        growth_time += global.time_now - global.ds_deactivate[# 1,i]; //change the time variable according to time_now minus the deactivation time
        ds_grid_set_grid_region(global.ds_deactivate,global.ds_deactivate,0,i+1,1,ds_grid_height(global.ds_deactivate)-1,0,i); //copy the grid up one column
        ds_grid_resize(global.ds_deactivate,2,ds_grid_height(global.ds_deactivate)-1); //delete the last column
        break;
    }
}

if point_in_rectangle(x,y,obj_player.x-750,obj_player.y-500,obj_player.x+750,obj_player.y+500) = false //in the deactivation range
{
    ds_grid_resize(global.ds_deactivate,2,ds_grid_height(global.ds_deactivate)+1); //add one column
    global.ds_deactivate[# 0,ds_grid_height(global.ds_deactivate)-1] = id; //record the id of the instance
    global.ds_deactivate[# 1,ds_grid_height(global.ds_deactivate)-1] = global.time_now; //record the deactivation time
    instance_deactivate_object(id); //deactivate the instance
}
 
Top