How to find a child in the tree to adjust it’s text via the script?
The details…
I’m creating a ‘basic window’ for all UI stuff. Within this ‘basic window’ I have some containers (box) which will be ‘filled’ on creation with a loaded resource (containing the buttons/sliders/etc.). This first part is programmed in the base_window.gd script with .
func _enter_tree():
if HeaderBoxLayout : $MarginContainer/VerticalBox/HeaderBox.add_child(HeaderBoxLayout.instantiate())
And this works like a charm. The nodes are added as intended. So I created a ‘Box’ scene which contains (in this case) 3 buttons. They are called ‘BtnLeft’/ ‘BtnMiddle’ and ‘BtnRight’ and each contain a default text with their name.
Now I created a scene calling the ‘base_window’ via a new script tower_extend_window.gd and this script extends the base_window.gd script. When I add a full path to the button, then I can update it. This path can get (very) long in complex windows so I want to be able to change it via looking for it (recursivily) in the tree. I’m trying this for the ‘BtnLeft’ button but I run into an error.
Thanks for the quick reply and suggestion. I watched the video (very informative) but I’m not at this stage yet. Your suggestion is about the use, but I’m trying to get childs ‘configured’. The 3 button scene will be used in multiple windows but should each have their unique text displayed for the function that they are going to do. So now they display ‘Left’/‘Middle’/Right’, but they should show a different text on the label eg. ‘Done’/‘Move’/‘Delete’. But in another instance the text could be ‘Start’/‘Stop’/‘Reset’. With the extended script I want to setup the desired texts (and your suggested signals).
As mentioned in the question a direct path is working, but I want it to search thru it’s childs based on a name in order to set the
I think you can do it with a class-based checker, in the main code of this repository.
You can also define a variable for each script that takes and holds the button from the previous script.
The problem I faced was lying in the fact that the find_child() did not find the child due to the owned parameter. Default it is true, but in my case it should be false. Probably because it is a separate loaded ‘child’ scene that is added to the own part.
Take a note on the super._ready() part. You need this in case the base_window script has its own _ready() part. Otherwise that will not be runned as it is overwritten by the extended script version. In my case this resulted in not setting the title text.