Deactivating a region

Le Slo

Member
Hello!

I'm trying to make a specific deactivating system because of some objects I have in my game should behave differently to this matter. I started trying to replicate the instance deactivate region function. So my code looks like this:

Code:
    var value=1;
    var NCX=obj_camera.camX;
    var NCY=obj_camera.camY;
    var NW=obj_camera.width;
    var NH=obj_camera.height;
    
instance_activate_region(NCX-NW*value,NCY-NH*value,2*value*NW,2*value*NH,true);
//instance_activate_all();
with(all){
    if(object_index!=obj_camera && object_index!=obj_player){
        if(x<NCX-NW/2 || x>NCX+NW/2 || y<NCY-NH/2 || y>NCY+NH/2 ){
            instance_deactivate_object(id)
        }
    }
}
I've banging my head with this simple thing. Each step the number of activated instances is different (there are only two values per position of the camera) even when the camera is not moving at all, and almost all the instances of the room are activated. What am I missing here?
 

Ubu

Member
Not sure why it isn't working. I tried your code and apart from some flashing instances (because of where you have defined the boundaries) it works just fine at my end (GMS2). I placed your code in the camera object. Do you use GMS2 with the new camera system? Have you double checked that your camera's width and height are correct?
Maybe add this:
show_debug_message(instance_number(all)-2); // -2 to not count player and camera
 

Le Slo

Member
Hey cool! Thanks for the answer!

Actually my main issue is the flashing instances, is there any way to avoid that? I have two main objects on my game, laserblocks and laser receptors. As the lasers of the laserblocks are made from particles and have infinite range; and because the position of the object doesn't give me that much information, I wanted to deactivate them with a different behavior that with the laser receptors:
-Laser receptors can be deactivated as long as they aren't seen by the camera.
-Laserblocks can't dissapear as soon as the leave the screen, because that may occur with a laser I'm seeing. That's why I wanted to deactivate them at least a bigger region.

I'm not sure how to approach to this problem:
a) Can I work with some kind of deactivate_region_layer? As long as I have both objects in different layers it looks easy. Not as efficient as it could be, but it's worth the try.
b) How can I handle the flashing of the instances that lay in the boundaries of my defined region?
c) Other ways to do this.
 

Ubu

Member
If I understand you correctly the only thing you have to do is to increase the boundaries of the region you're checking. This can easily be done by introducing an offset variable;
Code:
var offset = 64; // play around with this number to make it fit your need
and then add it to your check:
Code:
if( x<NCX-((NW/2)+offset) || x>NCX+((NW/2)+offset) || y<NCY-((NH/2)+offset) || y>NCY+((NH/2)+offset) )

You can also consider changing (not necessary though):
Code:
instance_activate_region(NCX-NW*value,NCY-NH*value,2*value*NW,2*value*NH,true);
to:
Code:
instance_activate_region(NCX-(NW/2),NCY-(NH/2),NW,NH,true);
just to reflect the check above and make the code slightly more readable.
 

Le Slo

Member
Thanks again, there are useful advises in your response. What I was trying to do (excuse me if I wasn't clear, I need to practice my english) is deactivate n different type of objects, using n different regions, at the same time. After rethinking my comment I found this answer:

Code:
var value=0.9;
var NCX=obj_camera.camX;
var NCY=obj_camera.camY;
var NW=obj_camera.width;
var NH=obj_camera.height;
    
instance_deactivate_all(true);
instance_activate_region(NCX-NW*value,NCY-NH*value,2*value*NW,2*value*NH,true);

value=0.55;

instance_deactivate_layer("Basics");
instance_activate_object(obj_camera);
instance_activate_region(NCX-NW*value,NCY-NH*value,2*value*NW,2*value*NH,true);
I hadn't yet inner knowledge of the layer, I mean they haven't pop up as a resource when I was trying to solve problems, I I haven't get used to them yet.
Actually you can do this for all the type of objects you want. Each type of object should be in a different layer, and the size of the region should always be decreasing.

I don't know if this is a good answer but at least I found a solution for now.
 
Top