+1 vote

I have a simple routine attached to my Player node to show/hide a menu screen when the Enter key is pressed. I can't figure out how to not have it disappear immediately, as the key press seems to register in multiple frames and trigger both "if" statements.

Is there a way to handle input like this without a timer node to space the input events?

func _process(delta):

    if Input.is_action_pressed("menu"):
        var menu = MenuScene.instance()

        if menu.is_inside_tree():
            menu.queue_free()

        else:
            add_child(menu)
in Engine by (15 points)

2 Answers

+2 votes

You want Input.is_action_just_pressed() instead. It's only valid on the first frame the input was pressed, so it's perfect for this case.

by (110 points)
+2 votes

you should not create an instance of MenuScene every time.

var menu = MenuScene.instance()

func _ready():
    menu.visible = false
    add_child(menu)

func _process(delta):
    if Input.is_action_pressed("menu"):
        menu.visible = !menu.visible

in another way

var menu = MenuScene.instance()

func _process(delta):
    if Input.is_action_pressed("menu"):
        if menu.is_inside_tree():
            remove_child(menu)
        else:
            add_child(menu)
by (9,736 points)
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 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.