using controller in UI

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rogerdv

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?

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.

Ertain | 2020-01-14 18:02

:bust_in_silhouette: Reply From: Zylann

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 :slight_smile:

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

rogerdv | 2020-01-15 20:34

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

Zylann | 2020-01-15 20:51