0 votes

Is there a way to prevent a node from loading from script, and then bring it in later as it would have loaded when playing a scene?

ie. the node the would be seen in the editor, but on playing the scene it would be in all senses not there.

Alternatively, maybe a way to have a parent node load, but withold its children from loading?

asked May 22, 2019 in Engine by blueSuedeShoes (32 points)
edited May 22, 2019 by blueSuedeShoes

What exactly do you mean with 'load': show/do things/exist/run/etc. There is a way, but the question is what do you want?

I suppose it would load just enough so that the game knows there is a node to be loaded, without actually loading it, still retaining it's structure and properties from the editor.

To contextualize it, I have a node with many hundreds of child nodes. The game runs fine with all these nodes loaded, but as the nodes load themselves at once (when the player is close enough) it causes a stutter. I figured a way to overcome this would be to stagger the loading of each of the child nodes.

I realize I could create, say, Position2D nodes as placeholders and load each child node into those placeholders with brief intervals, but it's extremely useful to see and edit these nodes within the editor.

You could have a look at the entertree() function. Different then _ready() it will execute as soon as the node enters the tree, thus before the child nodes entered. Maybe this is is useful?

You could also create placeholders as you called them, add them into groups of whatever you need and then as soon as they are needed replace them with real ones like:

var Box = preload("res://Items/Box.tscn")

func _on_area2d_entered(body): #nodes should be 'activated'
    for box_placeholder in get_tree().get_nodes_in_group("Boxes"):
        var new_box = Box.instance()
        # assigning some variables
        new_box.position = box_placeholder.position
        new_box.name = box_placeholder.name
        box_placeholder.queue_free()
        # ...
        # assigning all the other variables
        # ...
        remove_child(box_placeholder)
        add_child(new_box)

If that's a good solution depends on how many of those nodes would be loaded at once, I guess :)

Also: If you want to stop nodes working that are not on the screen VisibilityEnabler is something you should look at.

Hope I was able to help. If not feel free to ask further questions.

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.