When added as get_tree().add_child( inst ) stays invisible

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

Hello!

It is strange. I try adding scene instances by doing the following in _ready() method of my scene set to auto-load in project settings.

func _ready():
        var inst = preload( "res://scene.tscn" ).instance()
        get_tree().get_root().add_child( inst)

It stays invisible. I can’t see it. But if I do

func _ready():
        var inst = preload( "res://scene.tscn" ).instance()
        self.add_child( inst)

Then everything is fine. Why can it be? What can be wrong with adding child to the root? Godot version 3.2.1.

What is the parent’s class?

Tort | 2020-08-03 12:10

Can you show whole scene tree? Are you sure your inst should be child of the root? Do you have viewports in the tree?

Varden | 2020-08-03 13:22

The whole tree is the following:

Spatial
|_ Camera

And the script is attached to the Spatial. The get_tree().get_root() is Viewport, right? And self is Spatial

z80 | 2020-08-03 17:23

Made a few more tests. I’ve replaced the scene root element with MeshInstance to validate.
In the following scene

MeshInstance
|_Camera

I can see the Mesh instance via the Camera.

But! If I do get_tree().get_root().add_child( whatever ) or get_parent().add_child( whatewer ) it doesn’t show up. But self.add_child() works no problem.

So, if the get_tree().get_root() was invalid the scene’s MeshInstance also would be invisible. Right? But It is perfectly visible. But when anything is added to the parent of MeshInstance it stays invisible.

z80 | 2020-08-03 17:32

Try to run scene and check here.
enter image description here
It should work as you described. You can share your scene for me to test.

Varden | 2020-08-03 19:56

:bust_in_silhouette: Reply From: neighbour_l

Check instance global position. In first case it’s (0, 0). In second case instance position is determined by a parent.
You can also assign the instance a position of its own after you added it into tree.

I’m sorry, I don’t know how to attach images or files to the message!

Basically, what I don’t understand is the following. The topology is the following:

Viewport
|_MeshInstance
    |_Camera

Camera can see MeshInstance.

Now, if I do the following:

Viewport
|_MeshInstance2
|_MeshInstance
    |_Camera

I.e. MeshInstance2 parent is also Viewport. Camera cannot see MeshInstance2.

Viewport
|_MeshInstance
    |_MeshInstance2
    |_Camera

And in this topology camera can see both MeshInstance and MeshInstance2.

In all cases global positions of all MeshInstance-s are (0,0,0).

May I know why in 2-d case (both are parented to Viewport) Camera sees only its direct parent but doesn’t see parent’s sibling?

z80 | 2020-08-04 03:12

:bust_in_silhouette: Reply From: SXersi

Use call_deferred(). SOmething like this:

func _ready():
    var inst = preload( "res://scene.tscn" ).instance()
    call_deferred("add_to_root", inst)


func add_to_root(instance):
    get_tree().get_root().add_child( instance)

I tried this, but the scene was still not present when checked with get_tree().get_root().print_tree()'

Didnt matter if I tested in _ready() or _physics_process()

Just trying to instance a scene and add it to root in _ready, no autoloading present.

Jerry7 | 2022-12-12 20:54

It is necessary to print_tree() via call_deferred() as well.
Adding child to root (without call_deferred) works for me in _physics_process, but needs call_deferred() in _ready.

Hrom | 2023-06-05 17:24