Godot Version
4.3
Question
i want mob to detect player,
in one map have around 30 ~ 60 mob
now which one between area2d and is_in_group , that easy to handle and have better performance ?, can i know what pro and con between this two method?
4.3
i want mob to detect player,
in one map have around 30 ~ 60 mob
now which one between area2d and is_in_group , that easy to handle and have better performance ?, can i know what pro and con between this two method?
These are 2 completely different things that you mentioned.
Use Area2D
if you want to detect something in a specific place.
Use is_in_group()
if you want to check if the object you’re referencing is something you expect, meaning it is in the specific group.
These don’t have to be “one or the other”, these can easily coexist, even within the same script.
And I personally don’t like groups, so I can’t recommend it for anything. You can live happily without ever touching the groups.
And last advice - don’t make any decisions around performance if you don’t have performance problems, especially at the beginning of your gamedev journey. Just make stuff your own way and if it starts lagging - then open up the profiler, find bottlenecks and optimize them. You will learn a lot more by just making stuff and not worrying about issues before they arise.
Can you expand on what you don’t like about groups?
Main reason being that it’s not type-safe, because you’re referencing group names by string. Any typo you make will be a nightmare to find in a bigger project, because IDE can’t help you.
You can achieve almost the same behavior as is_in_group()
method by creating proper classes with class_name
keyword and then checking e.g. if node is Player
.
If you’re calling methods with call_group()
then you better pray that your node has this method implemented, otherwise again it’s gonna be super hard to debug if you made a mistake. You should be making proper references of Nodes you might want to call together, not trying to find them at runtime by group names scattered around your scene.
GDScript will hopefully soon have a Traits system implemented, which will make this whole system even more obsolete, along with my most hated method has_method()
.
I haven’t come across a problem yet where a group-based solution couldn’t be superseded by some other more type-safe alternative solution. So if you have any - I’d love the challenge