Is it safe to call move_and_collide multiple times per frame?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pennycook

I’m currently using KinematicBody2D to implement my Player and Enemy classes. The Enemy decides how it should move using a behavior tree, and I call move_and_collide inside one of the leaves of the tree.

I was pretty happy with the way that this was working until I started to try and implement some of my other game mechanics. I have an Area2D that tries to pull the KinematicBody2D towards its center, by calling move_and_collide every frame on any body that has entered the area.

This results in (at least) two calls to move_and_collide per frame – one where the Enemy tries to move away from the center of the Area2D, and another where the Area2D tries to move the Enemy back towards its center.

Is it safe to structure things like this, or is there some built-in assumption in the way Godot works that requires me to restructure things such that move_and_collide is only called for a given KinematicBody2D once per frame?

:bust_in_silhouette: Reply From: AlexTheRegent

Yes, it is safe to call move_and_collide multiple times per physics frame. This is actually how another built-in function, move_and_slide, works. It has argument named max_slides (default value is 4) that defines how many times per one call move_and_collide can be called (if during movement object collides with something).