There is something I wonder: when you say only once, is it only the first time any object is in the specified position? Only the first time a specific object enters that position? Once per game boot?
In any case, I think this is something that would be better controlled via Area2D and the body_entered
signal.
You place an Area2D in the desired position and connect its body_entered
signal to a function (it'll be, by default func _on_<FOO>_body_entered(body)
, where FOO
is the name of the child Area2D), and you can then do something like this
func _on_FOO_body_entered(body):
# Feel free to make any checks here, if you want a SPECIFIC object to trigger the sound, something like
# if body.get_class() == yaddayadda
$Audio.play()
You can add the same boolean handling already suggested if you really want it to be played once, but this way, you don't need to update the values of position if the position changes. Moving the Area2D will do that for you. I think it's a less error-prone solution, and a good way to practice signaling in Godot, which is awesome :D