This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hi,
I faced this really strange challenge with gdscript. I don't know if I really can't see the problem in this simple code or this is some kind of bug.
Here's the code where I want to increase the counter by pressing enter and then reducing it by pressing the button. Strange thing happens when I press enter two or three times, then reduce the counter by pressing the button. When I press enter once again, it runs the button function automatically after it. The counter gets up by 1 and drops by 1 just by pressing enter. I can't see any connection between the two, but maybe it's just me. Hopefully someone can try the code and help me understand what is going on here.

extends Node2D
var counter = 0

func _input(event):
    if event.is_action_released("ui_accept"):
        counter = counter + 1
        print("Space pressed")
        print(counter)

func _on_Button_pressed():
    counter = counter - 1
    print("Button pressed")
    print(counter)

UPDATE: Using different signal func _on_Button_gui_input(event) with the same code works as intended, but I still don't see the real reason why pressed signal is causing the problem in the first place.

Godot version 3.5
in Engine by (23 points)
edited by

1 Answer

0 votes
Best answer

I'd guess this is a focus issue. That is, when you click on the button with the mouse, the app's "focus" shifts to that button. From the point on, when you press Enter, it presses the button because it has focus. That behavior is controlled by the button's enabled_focus_mode property. If you set that to None, the button will no longer take focus and pressing Enter will no longer cause it to be pressed.

by (22,704 points)
selected by

Exactly. I knew it's something simple.
Thank you very much!

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 [email protected] with your username.