In the "Dodge the Creeps" tutorial, would it be a design faux-pas to call 'queue_free' on mobs from the HUD scene?

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

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?

:bust_in_silhouette: Reply From: kidscancode

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.

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.

beeftendon | 2020-09-27 01:16

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!

kidscancode | 2020-09-27 02:59