How to set focus neighbour button via code?

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

What is the right syntax to setting a focus neighbour via code?

I have a Final Fantasy stlye menu (vertical main menu on the left, item grid menu on the right). When I press the ui_right button, the focus transitions from the left menu to the right one, but then it is stuck there unless i press ui-down (which moves the focus out of the right grid menu and down on the left vertical menu). I want the focus to return from the right menu to the top button on the left menu.

Since the buttons in the grid menu are instantiated during runtime, I’d have to set focus neighbours via code, but neither of these variations did work:

get_child(0).focus_neighbour_left = NodePath("../../../LeftMenu/01Item")
get_child(0).focus_neighbour_left = "../../../LeftMenu/01Item"
get_child(0).set_focus_neighbour_left = "../../../LeftMenu/01Item"

They all give me function nonexistent in base button error :stuck_out_tongue:

Any help would be appreciated :slight_smile:

:bust_in_silhouette: Reply From: skysphr

It should be either

focus_neighbour_left = NodePath("../../../LeftMenu/01Item")


set_focus_neighbour(MARGIN_LEFT, NodePath("../../../LeftMenu/01Item"))

Note that since you are calling the function from within your parent node, the path that you are referencing should be relative to the parent, not the child.

Thanks a lot, with your help I got it to work :wink:

Tsune | 2021-12-08 00:50

:bust_in_silhouette: Reply From: idbrii

If you have two nodes, you can use get_path_to to simplify your path getting:

prev.focus_neighbour_bottom = prev.get_path_to(btn)
btn.focus_neighbour_top = btn.get_path_to(prev)

Great for assigning focus when building menus dynamically.

I think you could also use it with $ syntax:

$Buttons/Top.focus_neighbour_bottom = $Buttons/Top.get_path_to($Buttons/Border/Bottom)
$Buttons/Border/Bottom.focus_neighbour_top = $Buttons/Border/Bottom.get_path_to($Buttons/Top)