0 votes

I'm trying to create a method that I call between my splash and main menu to instantiate and record all spawn positions from my levels. I'm getting an !is_inside_tree() error, likely since I'm trying to iterate over children directly after adding the node to my tree.

func get_spawn_position_from_stage():
    for path in stage_paths:
        # instance level to get spawnpoint
        var stage_path = path
        var temp_level = preloaded_level_dict[stage_path].instance()
        var canvas_layer = CanvasLayer.new()
        canvas_layer.layer = -9999
        canvas_layer.add_child(temp_level)
        var point
        for child in temp_level.get_children():
            if child.name == "SpawnPoint":
                point = child.global_position
        canvas_layer.queue_free()
        print(point)

Any way around this? In short, I want to instantiate, iterate over, then free super quickly.

I tried embedding the iteration over children into a separate method that I use a call_deferred() to call, but no luck.

in Engine by (1,557 points)

Is there a reason why you need to add them to the tree? Do spawn points appear only in _ready or something? Because otherwise you could simply do a tree traversal and even save some time (a bunch of extra logic runs when a node enters the tree).

Otherwise, would be interesting to know which line causes the error.

Please log in or register to answer this question.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.