You need to ask yourself what are the requirements an entity must satisfy for it to be "targetable". Saying, "it must have the TargetableComponent
" isn't useful. You are really trying to answer, "what additional information and behavior is required for an entity to be considered targetable?"
If all the information you need out of a targeting system is a back-reference to the owning entity from which you can access the position, maybe you're better off putting together a list of PositionComponent
s for your targeting system. No really new information is being added, and the TargetableComponent
is just being used as a "marker component." That is, it exists solely to add a single bit of information to an entity: whether or not it is targetable. Marker components aren't inherently bad, but in most cases seeing them seems like a case of "to a hammer, everything looks like a nail."
In this case, I would have some sort of flag/tag system (a [Flags] enum could work well for this, you can have 64 different flags if you type it as a long), and arrange it such that I could quickly query an area of the game world for objects for which the following is true: (entity.Flags & EntityFlags.Targetable) == EntityFlags.Targetable
. If you need more than 64 tag/flag/whatevs, then that's what lists of tag IDs are for (they could just be hashes of the string name).
I'm assuming you need much more than one bit of information for your TargetableComponent
though. Maybe you need to send events when an entity becomes targeted, need to know how many other entities are targeting this entity, and all sorts of stuff. In which case, don't feel guilty for using this TargetableComponent
pattern.
Whether or not it's better to use marker components versus some sort of tagging depends completely on how you're planning to use it in your game. Do you want to dynamically check for the presence of TargetableComponent
on entities? How frequently are you doing that? If it's as frequent as every few frames, is that an expensive operation in your system (ie: Unity)? Are you pre-populating a list of targetable entities by scanning all entities at level load for those who have TargetableComponent
? Or does TargetableComponent
itself maintain a static fast-access list?
Bottom line: Keep doing what you're doing so you can make progress and better define your problem. If necessary, you can redesign this system later to better handle your requirements.