1

I have the following object types:

  • oWall
  • oEnemy

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.

Andrei Statescu
  • 329
  • 1
  • 6
  • 22

1 Answers1

1

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.

YellowAfterlife
  • 2,967
  • 1
  • 16
  • 24
  • @yellowAferlife thanks a lot for answer, exactly what I was looking for. The `array_push` thing was just a mistake in my initial examples, thanks for pointing this out. Also, got idea to check the original implementation and rewrite that. All things considered, inheritance seems to be the simplest way to go in the end, although I prefer to avoid it from a clean code perspective. – Andrei Statescu Jun 09 '23 at 18:48