UI Focus with a gamepad and instantiated scenes within a scene
Asked Answered
C

3

0



Hi there,

I'm quite new to Godot, having joined after a recent "other game engine mass-exodus"...

I have a HUD scene, which contains a VBoxContainer, into which I instantiate child-scenes, which are at the moment, HBoxContainer Nodes, filled with a variety of smaller controls - A couple of labels, a color picker, and a SpinBox. Each of these instantiated scenes represents a player added to the game's control selection.

The main HUD also has three buttons:

  • Add player
  • Remove player
  • Start

The Add player button instantiates a player's selection controls (HBoxContainer node scene as mentioned above) and places it as a child into the HUD scene's VBoxContainer. Of course, the Remove player button removes that node.

Start simply starts the game.

I have setup the focus settings to be able to easily navigate the Start, Add player, and Remove player buttons and select them with the gamepad. However, my trouble is that I am not able to focus and select the HBoxContainer nodes (and their child controls) when they are added to the HUD scene's VBoxContainer.

I have attached screenshots showing my HUD scene as well as the scene for player control selection (which is what is being dynamically added to the HUD's VBoxContainer). There can be a maximum of 4 x of these added (4 x players co-op) and when the game starts, I automatically add 1 of these for "Player 1".

How can I get UI focus selection to work across the three buttons in the main HUD as well as the dynamically added HBoxContainer's and their child controls?

Catchment answered 8/11, 2023 at 19:1 Comment(0)
M
0

Catchment Not sure I understand the issue correctly but have you already tried calling grab_focus() on one your input controls after adding them to the scene?

Mantis answered 8/11, 2023 at 19:58 Comment(0)
C
0

Mantis thanks for the reply. Yes, I've tried grab_focus() on one of the child controls after instantiating the scene - that works to allow me to switch between all controls in that node that was added, but I can't 'hop out' of that instantiated scene and down to the remove or add player / start buttons (or any other player control scenes added alongside that one in the VBoxContainer for that matter.

I guess what I'm missing is, the engine handles ui input events well between sibling control nodes, but doesn't know about neighbours that belong in other scene nodes and how to grab focus on those.

Catchment answered 8/11, 2023 at 21:48 Comment(0)
C
0

I think I've figured this out now. Though I would love to know if there is a better way.

I'm programatically assigning top/bottom neighbours for focus based on when my player control selection scenes are instantiated into the main HUD VBoxContainer. Essentially hooking things up in code.

So after I add a player control selection scene to the main VBoxContainer, in the HUD's class / script, I iterate over the children of the VBoxContainer, figure out if I'm on the first or the last index of the container, and if the first, I set the Add button as the Top Neighbour of the various controls in the instantiated scene. I do the same for the last indexed scene in the VBoxContainer, but set the Remove Button as the Bottom Neighbour.

The scenes themselves sort out neighbour focus automatically (I don't configure them to have neighbours at all through the Editor Inspector as I'm of course instantiating these dynamically).

If anyone stumbles on this thread, let me know if there is an easier way.

Catchment answered 8/11, 2023 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.