0 votes

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?

in Engine by (28 points)

1 Answer

0 votes

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

by (180 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 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.