Prevent a node, or its children, from loading

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

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?

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?

Jowan-Spooner | 2019-05-22 17:21

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.

blueSuedeShoes | 2019-05-22 18:16

You could have a look at the _enter_tree() 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 =
        # ...
        # assigning all the other variables
        # ...

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

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.

Jowan-Spooner | 2019-05-22 19:27