0 votes

Yesterday I started to test my game project using an Xbox One controller and after some tweaking I made the gameplay part work fine, but found that UI can not be used with it. How can I make my main menu buttons work with a controller?

asked Jan 14 in Engine by rogerdv (55 points)

A way of having a controller work with the GUI is to have your code in the _unhandled_input() function. To control the GUI a little finer, you can also use the Control._gui_input() function. Finally, you can even set how a Control responds to an input (such as "ui_accept") in the editor.

More information can be found here.

1 Answer

0 votes
Best answer

I will complement with some experience I had:
I made my game support a controller by using the focus state. When you click on a Button control, or a LineEdit, you may notice a blue circling appear and remain on it (unless you hided it). It means the control has the focus. i.e, it's the currently selected one.

Focus allows to use the keyboard to switch to previous or next control in the UI, sometimes using the arrows keys or the tab key. It does behave semi-automatically already, but you can specify which control to go for all 4 directions on each control, in the inspector.

When a new UI is shown, I usually define which control gets the focus first, using a script. For example, when going to the Settings screen, I call grab_focus() on the first option. I also made it so my UI understands the "Previous" action, usually bound to the Escape key on the keyboard, which makes it go to whatever previous UI, or closes it.

If you manage to get your UI working this way only with the keyboard, then a controller is easy: instead of arrow keys, you use the D-pad :)

answered Jan 15 by Zylann (21,603 points)
selected Jan 16 by rogerdv

Why isnt the UI handled with left stick like Unity (and many games) do?

I don't know, different defaults maybe? You can change them though.

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.