GMS 2 Array for Collisions HELP! [SOLVED]


Can someone help me simplify this into an array or something else? Even with years of experience with GMS I still struggle to understand simple arrays, especially after coming back to it since buying GMS2. I feel like this should be easy but everything I try doesn't work. If someone could give me a simple solution it might help me figure it out in the future. I have watched almost every YT video about arrays and for loops but still can't seem to make it work. I am not a smart man apparently. lol

This is the code I am using, it works but is very messy and I am certain that I should not be having this many && statements together.

//Move Left
if keyboard_check_pressed(vk_left) || keyboard_check_pressed(ord("A"))
&& !place_meeting(x - xMove, y, ob_cocol)
&& !place_meeting(x - xMove, y, ob_unitcol)
&& !place_meeting(x - xMove, y, ob_thincol)
    keyLeft = true;
else keyLeft = false;
I tried making them into the same array and calling each like this:

collision = [ob_cocol, ob_unitcol, ob_thincol]

and then using:

&& !place_meeting(x - xMove, y, collision[0, 1, 2]

but it only gets the collision of the first variable so obviously it cannot function written this way right?


If you want to do the array then you would need to iterate over the array:


var _collisionArray = [ob_cocol, ob_unitcol, ob_thincol];
var _arraySize = array_length_1d(_collisionArray);
var _collisionOccurred = false;

for (var _i = 0; _i < _arraySize; _i++) {

    if (place_meeting(x -xMove, y, _collisionArray[_i])) {
        _collisionOccurred = true;


keyLeft = !_collisionOccured;
Something like that. But honestly there isn't anything really wrong with using many '&&' operators, other than you might not like the way it looks.

Boolean operators in if statements will be evaluated with short circuiting. So they can be rather efficient even with alot of them.


If at all possible, I am certain that you should be using a parent. Create a new object called ob_parentcol, make those three objects children of it, then just do this:
keyLeft = (keyboard_check_pressed(vk_left) || keyboard_check_pressed(ord("A"))) && !place_meeting(x-xMove, y, ob_parentcol);
Or alternatively, I have a ready-made library made for this exact kind of setup. Look at the source code on GitHub if you want to know how it works, but behind the scenes it's basically what Roldy just described.


I know as is it isn't too bad but I will probably add more custom collisions into the game and I get confused easily. Thanks though! I mostly just want to get a better understanding of how to use them.