0 votes

Hi everyone !

I've been looking for a great tutorial on how to make a menu that is selectable with a gamepad. Everyone that I find is using the mouse, but my game is made to be played with a controller...

Thanks a lot ! !!

Godot version 3.3
in Engine by (47 points)

1 Answer

0 votes

The best way would be to setup Input mapping this way your gamepad buttons can be reassigned during gameplay and you can use the mouse/keyboard if the gamepad is not attached.

Now to answer your question really depends on how your menu is structured
Since you did not provide one, will use this setup as a demonstration
enter image description here

For all your Control nodes set the focus_next and focus_previous properties in the editor located under the Focus tab

On the MainMenu scene you can have a script like this and show/hide it as needed

func _show():
    .show() #important when overriding

    # grab focus of the first node
    $HBoxContainer/VBoxContainer/Menu options/Continue.grab_focus()

func _hide():
    .hide() #important when overriding
    set_process_input(false) #disable input for this node

func _input(event):
    var current = get_focus_owner()
    if not current: #should not happen but in case it does

    if event is InputEventJoypadMotion:
        if event.axis == JOY_AXIS_1: #vertical left stick
            if event.axis_value == -1.0: #full motion up
                var prev = get_node(current.focus_previous)
            elif event.axis_value == 1.0: # full motion down
                var next = get_node(current.focus_next)

    if event is InputEventJoypadButton:
        #the confirmation button is pressed
        #this will be JOY_SONY_X Playstation JOY_XBOX_A on Xbox
        if event.button_index == JOY_BUTTON_0 and event.pressed:
            if current is Button:
                current._pressed() # emulate button press
                #current.emit_signal("pressed") #emulate button press
by (6,264 points)
edited by
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.