How to find a child in the tree, with flexible added nodes, using extended script

Godot Version

4.3 Stable steam

Question

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.

extends "res://Content/ReusableAssets/BaseWindow/base_window.gd"

func _ready():
	$MarginContainer/VerticalBox/FooterBox/Box_3btn/BtnMiddle.text = "Move"
	$MarginContainer/VerticalBox/FooterBox/Box_3btn/BtnRight.text = "Delete"

	var ChildNode : Node
	ChildNode = find_child("BtnLeft")
	ChildNode.text = "Done"

On the last line I get an error although the childs are in my tree. What am I doing wrong?

use make unique by right click on the node then reference it in the script

also add the outside script on the autoload it will make all tree scene can

reference and can send or chage game on run time like game manger or game

score system also signal bus is good see this video

also try read how to build node and add it to the tree scene it will help alot

1 Like

Hi @lowwaels98

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

<child_node>.text = <my_desired_text>

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.

Ok, got it working :smiley:

extends "res://Content/ReusableAssets/BaseWindow/base_window.gd"

@onready var BtnLeft = find_child("BtnLeft", true, false)
@onready var BtnMiddle = find_child("BtnMiddle", true, false)
@onready var BtnRight = find_child("BtnRight", true, false)

func _ready():
	super._ready()
	BtnLeft.text = "Hallo"
	BtnMiddle.text = "Move"
	BtnRight.text = "Delete"

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.

1 Like