• 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!

How to check if certain number of objects are inside view?

TsukaYuriko

☄️
Forum Staff
Moderator
View coordinates and dimensions are stored within the view_x/y/w/hview (x, y, width, height) arrays. You can use this together with point_in_rectangle and a with statement called on the object you want to check for.
 

Nidoking

Member
collision_rectangle_list might be heavier-weight, but you can also create a list, call the function to get the number of instances of that object in the view rectangle, then destroy the list without needing to read it.

Even in a with loop, collision_rectangle is probably simpler than point_in_rectangle.
 

TsukaYuriko

☄️
Forum Staff
Moderator
collision_rectangle_list might be heavier-weight, but you can also create a list, call the function to get the number of instances of that object in the view rectangle, then destroy the list without needing to read it.

Even in a with loop, collision_rectangle is probably simpler than point_in_rectangle.
While indeed much simpler (unsure about efficiency), keep in mind that this will not work for instances with no collision mask as no collisions will be registered for them.

Meanwhile, point_in_rectangle will use precisely the x/y coordinate of the instance, disregarding the sprite or collision mask entirely - so that approach may also not be applicable depending on the exact circumstances.
 

kburkhart84

Firehammer Games
Something to consider...it may be more efficient to use simple x/y comparisons like old-school bounding box collision testing. I would also say it might be a better solution to have a separate object controlling it, and each time either the view moves or the objects move, have each one update itself, adding itself to the controller's data(of what is inside the box) and then removing itself if it leaves the box. You could in fact make this code all be in a single place by using parent/child objects.
 
Top