0 votes

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.isactionjustpressed("changetime") 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():
    pass

func _process(delta):
    update_players()

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:
        Globals.player_future.enable_player()
        Globals.player_past.disable_player()
    elif Globals.time == Globals.PAST:
        Globals.player_past.enable_player()
        Globals.player_future.disable_player()

func _input(event):
    if Input.is_action_just_pressed("change_time"):
        change_time()
        print("time changed")
Godot version v3.4.2
in Engine by (48 points)
edited by

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()
      etc...

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

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
        change_time()
        print("time changed")
    if Input.is_action_just_released("change_time"):
        button_pressed = false

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.