How do I delay an input in a script?

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

I want to load an instance of my inventory scene with the press of “i” and then close it again with the press of “i”. At the minute my inventory scene is loading and is visible but when I press i again i think it closes and then opens again instantly.

Here is the script:

Player script:

func _unhandled_key_input(event):
if Input.is_action_just_pressed(“open_inventory”):
var inventory = load(“res://Scenes//Inventory.tscn”).instance()
add_child(inventory)
print(“i”)
get_tree().paused = true

Inventory script:

func _unhandled_key_input(event):
if Input.is_action_just_pressed(“open_inventory”):
queue_free()
get_tree().paused = false

when I press “i” when the inventory is up it prints “i”

Cheers for the help.

:bust_in_silhouette: Reply From: AndyCampbell

Maybe you need a cool-down timer.

So when after open (or close) the inventory screen:

  1. create a global flag, for example is_active = false which will cause your script(s) to ignore input, for example
if Input.isactionjustpressed("openinventory") && is_active:
  1. start a short timer. On timer complete, set the flag is_active = true so input will be accepted again