In my ECS implementation, I use bit-wise operations (as described and illustrated in this thread) to tell an entity what type of components it currently consists of. So my Entity
class has the following member:
unsigned long m_CompMask;
The different component types are then defined as an enum
, whose member values are all power of two:
enum ComponentType
{
COMP_TYPE_UNKNOWN = 0,
COMP_TYPE_PERSON = 1,
COMP_TYPE_RENDERABLE = 2,
COMP_TYPE_MOVEABLE = 4,
[...]
};
Each time a new component is added to my entity instance, I do the following bit operation to update the mask (where newType is a member of the enum
mentioned above):
m_CompMask |= newType;
This approach enables me to efficiently check if a certain entity instance has a certain component (and hence might be relevant for a specific system), like so:
return (m_CompMask & type);
The limiting factor in this approach however is the m_CompMask
variable since it won't be able to handle more than 64 component types (if I increase it to unsigned long long
).
I don't expect to be needing more than that in my current project, but nonetheless I'd like to hear some ideas regarding alternatives that allow more than 64 types (as big games certainly need to) while still maintaining the ease and efficiently of those bit-wise operations as much as possible? Any ideas how this is handled in "proper big" projects?