Game Stats Component

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Game Stats Component

      I'm using Unity to create a game and trying to stick to their component architecture. I'm currently working on adding stats (ie HP, Damage, Armor, etc) to characters. I see three ways to do this through components:

      1) A CharacterStatsComponent object that contains just the stats with other components to use them. For example, a TakeDamageComponent that would get the CharacterStatsComponent to get the HP and Armor to use in the damage formula. I like that this separates the stats from the other logic (taking damage, doing damage, movement speed, etc), but not sure if I like having to couple those components.

      2) A CharacterLogicComponent (or similiar name) that contains the stats and the logic that uses those stats. This has the advantage of everything being in one place, but also the disadvantage that everything is in one place whether its related or not.

      3) The same CharacterStatsComponent as in 1, but instead of characters getting the various logic components (ie TakeDamageComponent) they instead have references to systems (ie TakeDamageSystem) that they pass the CharacterStatsComponent into. It would be similar to 1, but instead of having each character have multiple components there would be a single system they all use.

      I am leaning towards 1 or 3, but would like to hear others thoughts and reasoning.
    • I would go with option 4: create a different component for each area of responsibility.

      I don't think separating stats from the logic of that stat makes much sense. They're going to be tightly coupled anyway, why not just merge them? Personally, I would separate stats by functionality. For example, I might have a damageable component that handles hit points and potentially armor rating. Anything that can take damage gets this and all logic on that component is for handling damage. It's all self contained. That way, I can do this on a projectile:

      Source Code

      1. private void OnTriggerEnter(Collider other)
      2. {
      3. DamageableComponent damageable = other.gameObject.GetComponent<DamageableComponent>();
      4. if (damageable != null)
      5. {
      6. damageable.TakeDamage(m_damageAmount);
      7. DestroyObect(gameObject); // projectile is dead, so destroy it
      8. }
      9. }

      This would be on a projectile or a sword blade. Instead of looking for a tag or object name, you just check to see if the thing is damageable. If it is, you have the interface necessary to deal damage to it.