When two things move to touch one another, you do not know which one will detect the collision.
For instance, say you have two moving entities aligned and going in the same direction, but the front one is slower. Every time the back one moves, it collides with the front one, but when the front one moves, it stops colliding:
Initial:
A--> B->
A Moves and collides with B:
A-->B->
B moves without colliding with A:
A--> B->
To avoid that, you can make each moving Nodes implement a function, say on_collision(collision, with)
, and whenever one collides, they call their own function, and then the one of whoever they collided with:
for i in get_slide_count():
var collision = get_slide_collision(i)
on_collision(collision, collision.collider)
if collision.collider.has_method(on_collision):
collision.collider.on_collision(collision, self)
Physics related functions like move_and_... should always be called inside a _physics_process() or a function called inside one.