0 votes

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.

asked Aug 3 in Engine by z80 (31 points)

What is the parent's class?

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

The whole tree is the following:


Spatial
|_ Camera

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

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 gettree().getroot().add_child( whatever ) or getparent().addchild( whatewer ) it doesn't show up. But self.add_child() works no problem.

So, if the gettree().getroot() 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.

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.

1 Answer

0 votes

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.

answered Aug 3 by neighbour_l (14 points)

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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.