do something while body is in detection_area

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

Hello
I want to run a function(signal to be more accurate) for as long as my KB2D is in the detection_area

I tried to use while loop but it makes the engine to not respond and crush…
here is what I tried

func _on_ChasingDetection_body_entered(body):
{
if body.is_in_group(“player”):
x = 1
while x == 1:
move_to_player(body)
)

func _on_ChasingDetection_body_exited(body):
{
x = 0
motion.x = 0
}

note: I used brackets only here to make the question a bit more readable

thx

:bust_in_silhouette: Reply From: exuin

while loop will just run until it’s broken, which will be never in your case.

have a function that moves to the player. have an if statement in the process function that calls the function to move to the player if a certain variable like “player” is true. When the player enters the detection area set player to true or the player or whatever. When the player leaves the area set player to false or null.

Yeah I tried that but it won’t work because the body argument in the func onChasingDetectionbodyentered(body)

if I call this specific func in the process function it will show an error due to there are no arguments

godotuser123 | 2021-02-19 08:46

Don’t call it in the process function. If you connected the signal correctly it will be called when the signal is emitted.

exuin | 2021-02-19 08:57

I simply connectrd the on_body_entered signal…

godotuser123 | 2021-02-19 09:05

Okay, and did that fix it?

exuin | 2021-02-19 14:09

No bro
I can’t figure out a way to do it
the problem is the “body” paramter
if only I could use it outside of the signals

godotuser123 | 2021-02-19 18:31