SOLVED Having problems to identify multiple objects with a collision rectangle

Lens

Member
Hello, good morning/afternoon/evening!
I'm using a collision rectangle to identify other object instances around the room, and, while there is any of those specific objects touching/being inside the rectangle, the main character (whose posseses the collision rectangle) will change a variable ("collidedL") value in the Step event.


nomoveleft = collision_rectangle(x - 5, y - 85, x - 25, y - 5, ObjectA, false, false);
if nomoveleft != noone
{
collidedL = 1;
}
else collidedL = 0;


When collidedL = 1, he won't be able to move left, for example. Now, I have similar other objects (ObjectB, ObjectC...) that will do precisely the same (once in collision with nomoveleft recantle, will trigger the collidedL to be 1 aswell).
I have tried duplicating the code, changing the collision_rectangle object of focus to those other ones, however, when I do that, not only the code gets rather heavy (since there are quite a few different objects) but also the multiple rectangles tend to not work properly, since multiple instances of the same name are being run at the same time (I guess...?). Any ideas onto how to make it work?
 

saffeine

Member
when you duplicate the code, are you including the else part of it too? if so, there's one of the issues.
if you do use that method, it should be laid out like this instead:
GML:
collidedL = 0; // set it to 0, and have it be set to 1 if anything returns true.
//  previously this would have reset back to 0 if any of the checks failed.

//  each of these checks if collidedL is STILL 0. if it is, it won't continue with the check.
//  doing it this way will reduce lag from having to run all 3 checks even if one of them is already true.
if( !collidedL && collision_rectangle(x - 5, y - 85, x - 25, y - 5, ObjectA, false, false) ){ collidedL = 1; }
if( !collidedL && collision_rectangle(x - 5, y - 85, x - 25, y - 5, ObjectB, false, false) ){ collidedL = 1; }
if( !collidedL && collision_rectangle(x - 5, y - 85, x - 25, y - 5, ObjectC, false, false) ){ collidedL = 1; }
the better method though would be to create a new object and set the parent of all those objects to that new object.
by doing that you can just check a collision_rectangle for ObjectParent instead and reduce it to just the one line.
not sure if it's necessarily quicker under the hood that way, but it definitely makes it easier to handle.
 

Lens

Member
when you duplicate the code, are you including the else part of it too? if so, there's one of the issues.
if you do use that method, it should be laid out like this instead:
GML:
collidedL = 0; // set it to 0, and have it be set to 1 if anything returns true.
//  previously this would have reset back to 0 if any of the checks failed.

//  each of these checks if collidedL is STILL 0. if it is, it won't continue with the check.
//  doing it this way will reduce lag from having to run all 3 checks even if one of them is already true.
if( !collidedL && collision_rectangle(x - 5, y - 85, x - 25, y - 5, ObjectA, false, false) ){ collidedL = 1; }
if( !collidedL && collision_rectangle(x - 5, y - 85, x - 25, y - 5, ObjectB, false, false) ){ collidedL = 1; }
if( !collidedL && collision_rectangle(x - 5, y - 85, x - 25, y - 5, ObjectC, false, false) ){ collidedL = 1; }
the better method though would be to create a new object and set the parent of all those objects to that new object.
by doing that you can just check a collision_rectangle for ObjectParent instead and reduce it to just the one line.
not sure if it's necessarily quicker under the hood that way, but it definitely makes it easier to handle.
Thank you very much, it worked! Indeed using the object parenting made it extremely easier! As for the idea of latency reducing code line, it will definitely be usefull when I use more distinct objects (for another collision rectangle interaction that I will be soon working on...) Once again, thank you!
 
Top