#Copying entire scene tree to a variable

1 messages · Page 1 of 1 (latest)

lofty valley
#

How to copy scene tree to a variable, so a class that doesn't inherit Node can use GetNode<T>() and run stuff from nodes? When I tried, Godot sets variable to node without children.

heavy pumice
#

Just
SceneTree tree_reference = some_node.GetTree()

lofty valley
#

also tried that

#

Godot sets variable to node without children.

heavy pumice
#

The SceneTree is not the actual owner of the nodes, that would the Root of the tree.

But i don't really understand what is your goal here.

#

What did you mean with this exactly?
"Godot sets variable to node without children."

lofty valley
lofty valley
heavy pumice
#

There's only 2 reasons as to why it would have no children. Either the scene is empty at the time where you're checking (too early or during a scene change) OR the PackedScene loaded is simply empty.

lofty valley
#

fragment of code that assigns CurrentLevel to scene tree runs on _ready

#

reading the documentation it should work, because _ready runs when node and its children are ready

#

code that assigns is on topmost node

heavy pumice
#

If it is running in an Autoload, those are ready before the scene even loads.

lofty valley
#
public override void _Ready()
{
[...]
        //initalize evil scripting language and run ready function
        _scriptvm = new GodotVM() { CurrentLevel = GetTree() };
[...]
}```
#

GodotVM is

public class GodotVM : CeresVM
{
    public SceneTree CurrentLevel { get; set; }
}```
CeresVM is virtual machine of scripting language i'm using
#

when I try to access CurrentLevel from class that inherits from Node, it works

#

when accessing the same variable from class that doesn't inherit Node it doesn't

#

found my solution