FindNode in custom Control

Godot Version

v4.3.stable.mono.official [77dcf97d8]

Question

Hi everybody,
In my program, I have a 2D coordinates system that user can enter at different places in the UI. To achieve that, I created a scene that contains 2 spinboxes :

In the “_Ready” method of this scene’s script, I added the following lines to trace something strange that happens :

            GD.Print("SpinX : " + FindChild("Xspin"));
            SpinBox x = (SpinBox)FindChild("Xspin");
            if (x == null) {
                GD.Print("SpinBox X is null");
            } else {
                GD.Print("SpinBox X : " + x);
            }

When I run my project using this scene as default scene, Everything is OK as shown by the logs :


SpinX : <SpinBox#24914167056>
SpinBox X : <SpinBox#24914167056>

My problem is that, when I try to add this scene as a control in another scene using script, by creating an instance of the control and adding it to a container, scene’s inner nodes are not found :

SpinX : 
SpinBox X is null

Am I missing something ?

EDIT :
I tried to had another log trace :
GD.Print("in PositionControl._Ready : Control has " + GetChildCount() + " child(ren)");
and the behavior is the same :

  • When control is directly put in another scene by scene designer, it Ok (log says it has 4 children).
  • When control is created and added by script, log says it has 0 child

I’m wondering if it’s not an “event” problem : may be “_ready” is not the right place to check scene children when added by script… :face_with_diagonal_mouth:

Try use GetNode is more recommended

Thanks for the reply, but it doesn’t change anything.

How " is created and added by script"?
You can try use [Export] is was worked when i saved scene with exports inside

I want to make a “Generic Form” in which I can add fields depending on the content I want to edit. So, I made a scene with an empty VBoxContainer in which I add my fields by script in the _Ready method of the generic form like that :
container.AddChild(new PositionControl());

About Export, all I see is “Export as glTF 2.0”. Is it that you’re talking about ? If so, how do I use it afterward ?

new PositionControl() is not gonna create new children’s, you needed use GD.Load(" ")

1 Like

Thanks a lot !!!
Thanks to your reply, I managed to do it. If someone is having the same issue, the solution is :

PackedScene res = (PackedScene)GD.Load("res://scenes/PositionControl.tscn");
PositionControl control = (PositionControl)res.Instantiate();
1 Like

or like this :wink:

var control =  GD.Load<PackedScene>("res://scenes/PositionControl.tscn").Instantiate<PositionControl>();
1 Like

Much better with typed methods !

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.