How to check a variable in a script once per frame/physics change

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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"):

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")

    # 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

    # Remove Player Inherited Scene

    # Toggle Car HUD

The full source code can be reviewed here: GitHub - kingthrillgore/carposessdemo

:bust_in_silhouette: Reply From: Cyber-Kun

I’m not exactly sure if this will help but what if you check for input button down
make sure not is button pressed

That worked, more specifically I used Input.is_action_just_pressed()

Cameron Kilgore | 2023-03-28 17:53

yeah otherwise it will detect that the button is pressed every time it checks for Input.is_action_pressed()

Cyber-Kun | 2023-03-29 10:57

wich means if you hold it down it’s gonna detect that it’s held down and keep running the interact() function

Cyber-Kun | 2023-03-29 10:59