Advice for approach for status conditions. Examples provided.

Hey,

I’d like some advice on approach to build status conditions. what would be the better approach here:


Option 1:
To have a script called PoisonBehaviour with an update in it to remove health and a duration timer. This would be attached via code when it’s applied to an enemy / target game object and destroyed / removed when the timer runs out.


Option 2:
To have a script called PoisonBehaviour that is added to a PoisonPrefab and instantiated in the game and attached to enemy gameObject. Similar to above, PoisonBehaviour has a remove health and duration timer on update.

Option 3:
I have a List in the CharacterStats script for status effects. PoisonEffect has an IStatusEffect interface and can be added to the statusEffects list when applied. PoisonEffect has  a remove health and duration timer on update.

Option 4:
Have a single StatusEffectBehaviour script with all of the behaviour functions for all status effects in it, that is attached to all enemies / player.
When an effect is applied it runs through a switch to identify what function to run.

may i suggest a 5 option? i would surely avoid the first 2 options seems a bit messy, and unless all the interface status effects have some sort of persist during time effect i dont really see how having the list of status may help you organize the code. i would go for osmething like this, a parent class for player and enemy with public actions somehting like this

public event Action OnPoisonus = delegate { };

so whenever your character or enemys get poisonus you just need to call the onpoisonus method

enemyInstance.OnPoisonus();//or player

just make sure your methods gets notified when you call the onpoisonus event

private void Awake()
{
     OnPoisonus += StartPosisonusRoutine;
}

public void StartPosisonusRoutine()
{
     StartCoroutine(PoisonusRoutine());
}

 IEnumerator PoisonusRoutine()
{
      float timer = 0;
      while(timer < POISONUS_DURATION)
      {
          timer += Time.deltaTime;
          //poisonus effect
          yield return null;
      }
}