How can I conditionally stop a node from being added?

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

I have an obstacle which the player can destroy. Once it’s destroyed, the save file is updated so that the next time that room is loaded, I can check whether to load that obstacle or not.

That last part is giving me trouble. Currently, I am calling queue_free() in the obstacle’s _enter_tree function as follows:

func _enter_tree():
	if SaveManager.is_obstacle_cleared():
		self.queue_free()

This works for now, but I can see it becoming a problem. It’s a queue_free so it and its children will have all their _ready callbacks called, which I can see being a problem for more complex scenes. Calling free in _enter_tree crashes with no information, and I don’t think you’re supposed to do that anyway.

I’ve considered making the obstacle a child of a custom “ConditionalLoader” node or something like that, which would instantiate the obstacle at runtime depending on what the save file says. That means I won’t be able to edit the child in the scene it’s going to appear in, and in this particular case that’s a dealbreaker.

My ideal solution would be if I could override some theoretical should_enter_tree callback, but if anything like that exists, I couldn’t find it.

Are there any better solutions than what I’m doing at the moment?

try having the if statement before adding the node?

Nonken | 2022-08-07 13:17

The node isn’t added directly through code, it’s part of the scene which gets instanced when changing scenes. I suppose I could make a customized change_scene which checks that sort of stuff… I’ll give it a shot.

andrej88 | 2022-08-07 13:45

:bust_in_silhouette: Reply From: SteveSmith

Go with your current solution. Don’t worry about it being a problem until it’s a problem.