[CLOSED] Activating specific objects within a region

T

tamation

Guest
Is there any way to activate only one object when it's within the view region? I know there's instance_activate_region, but that seems to just blindly activate ALL instances within a region. I want something similar to that, but where it's only activating one type of object.

This is all I have right now in the code for my script:

///Deactivate all instances of solid collisions
instance_deactivate_object(oSolid)

I tried using distance_to_point in oSolid's step event to check where the player is so it knows whether or not to be active to see if that would somehow work, but that didn't really work out for fairly obvious reasons. :p
 

Simon Gust

Member
You could activate all of that object and then loop through the instances and see if they are outside the view and deactivate those again
Code:
// resurrect oSolid
instance_activate_object(oSolid);

// list all instances of oSolid
var inst_count = instance_number(oSolid);
for (var i = 0; i < inst_count; i++)
{
 var inst = instance_find(oSolid, i);
 if (!point_in_rectangle(inst.x, inst.y, view_xview, view_yview, view_xview+view_wview, view_yview+view_hview))
 {
  instance_deactivate_object(inst);
 }
}
 
T

tamation

Guest
You could activate all of that object and then loop through the instances and see if they are outside the v
iew and deactivate those again
Code:
// resurrect oSolid
instance_activate_object(oSolid);

// list all instances of oSolid
var inst_count = instance_number(oSolid);
for (var i = 0; i < inst_count; i++)
{
 var inst = instance_find(oSolid, i);
 if (!point_in_rectangle(inst.x, inst.y, view_xview, view_yview, view_xview+view_wview, view_yview+view_hview))
 {
  instance_deactivate_object(inst);
 }
}
I tried this, but there's an error with the code.

The error message was:
Variable <unknown_object>.y(1, -2147483648) not set before reading it.
at gml_Script_scr_render (line 9) - if (!point_in_rectangle(inst.x, inst.y, view_xview, view_yview, view_xview+view_wview, view_yview+view_hview))

I tried looking for why it wasn't reading the variable, but couldn't see any obvious issues. The inst variable was set one line prior with:
var inst = instance_find(oSolid, i);

Right now the code is just in a script that's executed every second in the step event. It works perfectly fine, until the view scrolls and new solid collisions should be activating.
 
T

tamation

Guest
Can you show the whole step event?
There's an object called oSolidRender, it's persistent and in the step event it executes a script. The script just has the above code in it.
I looked into it a bit, and it seems you can't use any of the instance_deactivate commands in conjunction with instance_id, as the second an object is deactivated it's removed from memory. I'll have to try and find another way to accomplish this or just rework some existing systems to use the traditional method. Thanks for your help though!
 
Top