How to set Focus Neighbour for embedded scenes

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

Hi there,

For reusability I have made a custom checkbox and saved it as a scene CustomCheckBox.tscn.
It is built like this:
-CenterContainer
–CheckBox

I’ve added this custom checkbox to my MainScene multiple times.

The problem is that setting the Focus Neighbour Left, Right, Top Bottom values on any of the CustomCheckBox instances in the MainScene doesn’t do anything.

I’ve realized that changing the Focus Neighbour setting on the instance of a CustomCheckBox in the main scene is only passed to the root node of the CustomCheckBox scene which is the CenterContainer in this case.
To fix this I’ve added a script to the CenterContainer of the CustomCheckBox scene to pass down the values to the checkbox control:

extends Control

func _ready():
    var cb = get_node("CheckBox")
    cb.focus_neighbour_left = self.focus_neighbour_left
    cb.focus_neighbour_right = self.focus_neighbour_right
    cb.focus_neighbour_top = self.focus_neighbour_top
    cb.focus_neighbour_bottom = self.focus_neighbour_bottom

This sets the focus neighbour properties, but when there is a focus change in a direction that has a value I get this error:

E 0:00:04.705 get_node: Node not found: …/CustomCheckBox_c.
<C++ Error> Condition “!node” is true. Returned: __null
<C++ Source> scene/main/node.cpp:1381 @ get_node()

I guess the problem has to do with how nodes see eachother’s path at runtime in different scenes but I don’t know the solution.

Could anyone help?

:bust_in_silhouette: Reply From: exuin

First off, if you right click on your instance in the node tree and check “Editable children”, you will be able to set the focus neighbors of the check box directly.

Second, it’s possible that not all the nodes are ready when the ready function is called, causing that error. You’ll know if all the checkbox instances are ready when their shared parent’s ready function is called.

Setting the “Editable children” was all I needed. Thanks!

zorkot | 2021-04-19 19:43