Using groups instead of booleans

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

Hi, recently I realized that instead of using boolean variable, we can just use groups, example:
Let’s say we have player and enemy. If player has sword, enemy runs away. If not, enemy attacks player. In the player script, we can declare:
var has_sword : bool = true
Then enemy checks that:

if player.has_sword:
   escape()
else:
   attack()

But instead, we can just add the player to group “has_sword”, and then enemy checks:

if player.is_in_group("has_sword"):
   escape()
else:
   attack()

I’m just wondering, if one of this options is more efficient than the other (consumes less RAM). Also, imagine we have 15 global variables in code, and every of them can be replaced with group, so maybe we can have smaller code (we don’t declare groups on top). What do you think?

:bust_in_silhouette: Reply From: Vrzasq

In that case I would stick to booleans. Its way faster than a method call. Groups can be used when You have general operation for execute on an entire group of nodes - saving game state would be a good example or detecting collision with certain object group. Properties like hassword or canmove describe object state and I think its not a good idea to move that into groups