I'm attempting to create an entity-component system in C++.
However, I've run into an annoying issue where a component system function inherently involves a lot of ceremony and boilerplate. This is because each function needs to perform a lot of validation before the actual meaningful code can be executed.
Example:
void cmpnt_enemy_damage(cmpntEnemy* cmpnt_enemy, int damage_amount, vec2* force)
{
// Validation starts here
cmpntPhys* cmpnt_phys = component_get<cmpntPhys>(cmpnt_enemy.ent);
if (IS_NULL(cmpnt_phys))
return;
cmpntCombat* cmpnt_combat = component_get<cmpntCombat>(cmpnt_enemy.ent);
if (IS_NULL(cmpnt_combat))
return;
cmpntAnimator* cmpnt_animator = component_get<cmpntAnimator>(cmpnt_enemy.ent);
if (IS_NULL(cmpnt_animator))
return;
// Actual function starts here
cmpnt_animator_set_animation(cmpnt_animator, cmpnt_enemy.hurt_anim);
cmpnt_phys_impulse(cmpnt_phys, force);
cmpnt_combat_damage(cmpnt_combat, damage_amount);
}
That's a lot of necessary validation for just 3 lines of code.
I was wondering if this is a common problem and if there are any well known solutions. Or is this just a necessary evil when using an entity-component system?
Thanks.