game-makergame-maker-studio-2

Using move_and_collide with multiple objects in GameMaker 2


I have the following object types:

And I'm trying to call the new move_and_collide function in such a way that it would collide against both of them. Something along the lines of:

objectsToCollide =  array_push(array_create(0), oWall, oEnemy);
move_and_collide(xSpeed, ySpeed, objectsToCollide);

I know that a viable solution is the make a new parent object that oWall and oEnemy could inherit from, but I'm hoping to avoid that since it would add unnecessary entities in my code.


Solution

  • First, array_push doesn't return anything and you don't have to use it like that either - you can do the following (manual):

    objectsToCollide = [oWall, oEnemy];
    

    The other thing is that move_and_collide does not support being given an array of objects to check against, at least not as of writing this.

    You could implement it yourself (you can see how the built-in function works here - it's just math and calls to instance_place) and make each instance_place call a loop over the array of instances instead, or consider whether you need move_and_collide for enemies - perhaps you could move against walls and then revert to previous position if it turns out that the player has ran into an enemy.

    And if you can straight up stand on an enemy, making it a child of oWall sounds reasonable enough.