+1 vote

This is more of a design principles question based on the "Your First Game"/"Dodge the Creeps" tutorial from the official docs: https://docs.godotengine.org/en/stable/getting_started/step_by_step/your_first_game.html

The tutorial instructs us to call get_tree().call_group('mobs', 'queue_free') in Main.game_over, which causes the mobs to disappear as soon as the player gets hit. However, I thought it'd be nice to have the mobs continue to exist until the title screen reappears. I've done this by simply moving the same queue_free call to HUD.show_game_over after the timer, as shown below:

func show_game_over():
    show_message("Game Over")
    # Wait until the MessageTimer has counted down.
    yield($MessageTimer, 'timeout')
    get_tree().call_group('mobs', 'queue_free') # Moved from Main.gd
    $Message.text = 'Dodge the\nCreeps!'
    $Message.show()
    # Make a one-shot timer and wait for it to finish.
    yield(self.get_tree().create_timer(1), 'timeout')
    $StartButton.show()

This accomplishes what I want, and it does seem that one of the uses of call_group is its ability to communicate between scenes. However, it feels intuitively wrong to have the HUD scene responsible for removing non-HUD objects. Is my concern here valid, or is this a valid design practice in Godot?

in Engine by (16 points)

1 Answer

+1 vote
Best answer

It's fine. But if you're really concerned by this, emit a signal from the HUD that notifies the main scene that it's time to clean up the mobs.

But in practice, it's really not something to worry that much about. There is no design cop who's going to hassle you for not doing things the "right way". It's your code, do it how you like. "Best practices" are just things that tend to work well, they're not hard rules.

by (19,695 points)
selected by

Thanks for the suggestion! I briefly tried signaling between scenes but it wasn't obvious to me how to implement that. I've since searched around and found some more discussions around it, so I'll play with that next time I have the chance. Agreed that best practices aren't hard and fast rules, but I think they're still good to keep in mind to help avoid writing confusing spaghetti code. This is my first foray into game dev/Godot so I don't have a good intuition yet how confusing things can get when functionality like this isn't well organized.

Intuition is something you develop over time. When you're starting out, don't be afraid to make mistakes and do things the "wrong" way. Making something that works, no matter how spaghetti-like the code, is a good thing!

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.