0 votes

The title says it pretty well, I need to be able to hover over a button in my project but not press it when it's in this state. Is this possible in godot?

in Engine by (207 points)

2 Answers

+1 vote
Best answer

This is possible, but it's harder than it should be. There's something unexpected (to me) about how the _Input(InputEvent @event) function is called. It appears to be always called, even when it seems like another Control should have handled the event. To work around this I added a mouse cursor position check.

Add a script to your Button that tells Godot that the mouse pressed event has been handled. Example in C#:

public override void _Input(InputEvent @event) {
    if(@event is InputEventMouseButton) {
        InputEventMouseButton mouseEvent = @event as InputEventMouseButton;
        if(mouseEvent.Pressed && GetGlobalRect().HasPoint(mouseEvent.GlobalPosition)) {
            GetViewport().SetInputAsHandled();
        }
    }
}
by (866 points)
selected by

Thanks for the help! I have 0 familiarity with C# though, aside from gdscript I've only ever used python and JS. Could you break down the logic for me, or do you know how much of this transfers to gdscript?

+1 vote

Based on the other answer, but a bit simpler:

func _input(event : InputEvent) -> void:
    if is_hovered() and event is InputEventMouseButton:
        get_viewport().set_input_as_handled()

warning:
i still can get focus and pressed via keyboard!

by (1,534 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 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.