+1 vote

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

Any help would be appreciated :)

Godot version 3.4
in Engine by (73 points)

2 Answers

0 votes
Best answer

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.

by (1,305 points)
selected by

Thanks a lot, with your help I got it to work ;)

0 votes

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)
by (95 points)
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.