• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker SOLVED Deactivating Instances Outside of Camera

F

foxroar1

Guest
Hi everyone,

I'm using the latest version of GMS2.

I'm creating a Mario game and I'm trying to deactivate everything outside of the camera view.

The room has viewport 0 enabled and visible with the camera properties width of 400 and height of 300. It's following my obj_p1, with a horizontal border of 200, vertical border of 150.

The obj_controller is following obj_p1 exactly.

CREATE EVENT
Code:
vx = camera_get_view_x(view_camera[0]); //camera x
vy = camera_get_view_y(view_camera[0]); //camera y
vw = camera_get_view_width(view_camera[0]); //camera width
vh = camera_get_view_height(view_camera[0]); //camera height
alarm[0]=1; //activate area
STEP EVENT
Code:
vx = camera_get_view_x(view_camera[0]); //camera x
vy = camera_get_view_y(view_camera[0]); //camera y
vw = camera_get_view_width(view_camera[0]); //camera width
vh = camera_get_view_height(view_camera[0]); //camera height

if (instance_exists(obj_p1))
{
x=obj_p1.x;
y=obj_p1.y;
}
ALARM 0 EVENT
Code:
instance_deactivate_all(true); //deactivate all
instance_activate_region(vx,vy,vx+vw,vy+vh,true);
alarm[0]=5;
It should be only activating objects within the view. I've checked the left border and it works correctly, but the right border doesn't deactivate objects immediately outside the view. Goombas still come towards me even if they're off screen. I've tried absolutely everything I can think of. I've tried inputting the actual view width of 400, I've changed it to 350 but it's never exact to where I think it should be activating/deactiving objects.

I've also tried putting all this code in obj_p1, or using an obj_p1 or the controllers exact x/y location but it never works.

I appreciate the help,
-foxroar1
 
N

NeonBits

Guest
if (distance_to_object(objplayer || objcamera) > (num) {instance_deactivate_object(obj) (or region);}?
 
Last edited by a moderator:

Jakylgamer

Member
in the enemy make it deactivate itself if outside the view. (this way it will be sure to deactivate them)
obj_enemy : outside view 0
Code:
instance_deactivate_object(id);
then all you need to do is activate the region in the view
obj_player: alarm 0
Code:
var vx = camera_get_view_x(view_camera[0]);
var vy = camera_get_view_y(view_camera[0]);
var vw = camera_get_view_width(view_camera[0]);
var vh = camera_get_view_height(view_camera[0]);

instance_activate_region(vx , vy , vw , vh , true);

alarm[0] = 3;
 
F

foxroar1

Guest
I believe I'm happy with that workaround solution, thank you Jakylgamer! It's not the way I envisioned doing it, I never imagined spending 3+ frustrating hours on this when it seemed so simple. I'm glad there's always more than one way to solve a problem.
 
F

foxroar1

Guest
By the way, for anyone that ever stumbles upon this thread, putting the instance_activate_region in an alarm did not solve the problem. The enemies were still activated off screen. Once I put the instance_activate_region in the step event everything works properly now.
 
Top