How efficient are methods on groups?

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

Greetings! Thanks in advance for reading this!

I’m making a small game and I’ve been leaning pretty heavily on groups for making set pieces, tilemaps and enemies visible/invisible as my level demands it. It’s an awesome feature and a huge convenience for me.

Right now I’m working on a function that will refresh the UI whenever the player gets an upgrade that would warrant adding an icon/item to the HUD (this is a Metroidvania, so upgrades would be far apart). Since the GUI is made of several items, I was thinking of making things easier on myself and using call_group on a “UI” group that will tell every UI element to refresh itself. I’m not familiar with how groups work under the hood, and I’m not sure if that’s a waste of resources. I could write each upgrade manually (the upgrade method finding and activating the required HUD element specifically instead of this general solution), but I don’t want to waste time if I don’t have to.

Is call_group a reasonable solution to this?

:bust_in_silhouette: Reply From: njamster

I don’t want to waste time

Then stop worrying! If this becomes an issue, you’re definitely doing something wrong.

I’m not familiar with how groups work under the hood

You can find the relevant function here. It uses a map-structure to look up which group-object is associated with the group-name, then loops over all the nodes associated with that group and calls the function. No black magic involved.