Input.is_action_just_pressed happens twice even when it's in _input function.

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

I have a time variable with a value that can either be FUTURE or PAST and I have a function that changes the value from FUTURE to PAST or PAST to FUTURE, I want that function to get called whenever I press a button with Input.is_action_just_pressed(“change_time”) but when I do the function gets called twice which returns the time variable to it’s original value. Even if I do that in _input function it still get’s called twice.

extends Node2D

func _ready():

func _process(delta):

func change_time(time : int = -100):
	if time != -100:
		Globals.time = time
	if Globals.time == Globals.FUTURE:
		Globals.time = Globals.PAST
	elif Globals.time == Globals.PAST:
		Globals.time = Globals.FUTURE

func update_players():
	if Globals.time == Globals.FUTURE:
	elif Globals.time == Globals.PAST:

func _input(event):
	if Input.is_action_just_pressed("change_time"):
		print("time changed")

What inputs are you using for the change_time action?

There’s a hacky solution you can use (which you shouldn’t need to!) but you can save OS.get_ticks_msec() in a variable when your input event is called, and then check if enough time has passed since the last input…

var lastInput
func change_time():
    if abs(OS.get_ticks_msec() - lastInput) > 200:
      lastInput = OS.get_ticks_msec()

rossunger | 2022-02-13 00:35

Try using event.is_action_pressed("change_time") instead.

samildeli | 2022-02-14 14:17

When i have came across this problem and I’ve always solved it like this:

func _input(event):
    var button_pressed = false
    if Input.is_action_just_pressed("change_time") and button_pressed == false:
        button_pressed = true
        print("time changed")
    if Input.is_action_just_released("change_time"):
        button_pressed = false

LittleTing1 | 2022-02-19 17:42