0 votes

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?

Godot version 3.5
in Engine by (65 points)

try having the if statement before adding the node?

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.

1 Answer

0 votes

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

by (1,053 points)
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 Frequently asked questions and 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 [email protected] with your username.