Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Cameron Kilgore |
I am not sure if I am asking this correctly, but here’s the issue i’m encountering:
I am trying to add the ability to possess/unpossess another scene, in this case a vehicle. I track the status of if its occupied using a boolean, and update it when the keypress is thrown. The issue is, this state appears to remain consistent between frames/physics updates, so the events of the interact() function throw many times, adding several player scenes.
I’m a bit confused how to best address this issue. What would be the best approach to guarantee this function only calls once when pressed?
The following code is fired on keypress in _physics_process
:
if Input.is_action_pressed(&"interact"):
interact()
And the interact() method
func interact():
print("Interacted with car")
# Check if player is posessing car already
if is_vehicle_active:
print("Player already posesses the vehicle")
#free_car()
# Change Camera
camera.current = true
# Posess Car
is_vehicle_active = !is_vehicle_active
# Unposess player
var scene_tree = get_parent().get_parent().get_parent().get_node("Player") # TODO use Signal
scene_tree.toggle_interact()
# Remove Player Inherited Scene
scene_tree.queue_free()
# Toggle Car HUD
get_parent().get_parent().get_parent().get_node("Spedometer").show()
The full source code can be reviewed here: GitHub - kingthrillgore/carposessdemo