body_exited not working.

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

Hey. I’ve just ran into a problem while trying to make a pressure pad button in a 2D platformer.

The button should “activate” when a body enters the Area2D node and “deactivate” when no bodies are within the Area 2D node. So far I am able to get the activation part to work with: _body_entered but unable to get anything to work with _body_exited:

`extends Area2D

var isactive = false

signal on
signal off

func _on_Area2D_body_entered(body):
if body.get_name() == “Player”:
isactive = true
print(body.get_name())
print(isactive)
emit_signal(“on”)

func _on_Area2D_body_exited(body):
if body.get_name() == “Player”:
isactive = false
print(isactive)
emit_signal(“off”)`

Basically I’m wanting the “isactive” variable to swap back to false when the body leaves the Area2D. As it is now it swaps from false to true when the player enters But does not swap back to false when the player leaves.
Any suggestions for how I could write this correctly or better would be appreciated. Thanks for your time.

:bust_in_silhouette: Reply From: timothybrentwood

You need to set your is_active variable to false inside of your on_Area2D_body_exited(). This is a copy and paste error.

Oh yep whoops. Had that set to true while testing and forgot to change it back. Thank you for letting me know.

However, initial issue still occurs.

Aosa | 2021-11-30 12:29

Your issue is not in the code you posted then. If your code in the body_entered works, I see no reason why your code in the analogous body_exited would not work. I can assure you that both the body_entered and body_exited signals work as intended. Your issue might be in the callbacks to the signals you emit or where you use is_active.

timothybrentwood | 2021-11-30 16:01