Not sure if this is the best/efficient way of doing this but you could use the _process()
function. Any code in the function is executed every frame and you can enable or disable it using set_process()
.
The code could look something similar to this:
func _ready():
set_process(false)
func _process(delta):
if Input.is_action_just_pressed("next"):
# Do stuff
func _on_Area2D_body_entered(body):
set_process(true)
func _on_Area2D_body_exited(body):
set_process(false)
Note that when you override _process()
in your code, processing gets enabled automatically, so if you want the object NOT to start processing right off the bat (AKA when it gets created), you should disable it in the _ready
function.
So we disable _process()
in the beginning. When the body enters the shape, we enable _process
and listen for input. When the body leaves, we stop _process()
.