Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Implementation Suggestions?

Discussion in 'Scripting' started by DRRosen3, Apr 11, 2015.

  1. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Looking for other ideas on how to implement our Status Effect system. Players (and enemies) can be afflicted by several status effects at a time. For example: Attack Down 25%, Defense Down 10%, and Speed Up 50%. At the same time, they can be afflicted by multiples of the same status effect at a time. For example: Attack Down 25%, Attack Down 25%, Speed Down 10%, Defense Up 50%, Speed Down 25%.

    Status effects wear off over time. So Attack Down 25% may last for 30 seconds. We were thinking of implementing this by having a script for each possible status effect, and applying the script via AddComponent() during runtime and having the scripts Update() tick down the duration and then remove itself. However, this could potentially result in dozens of attached scripts running updates at a time.

    Any suggestions?
     
  2. Xavior87

    Xavior87

    Joined:
    Feb 22, 2015
    Posts:
    23
    You can first check to see if the component is already attached.
    Then, if it isn't already attached, you can add it to the selected Gameobject. From this a boolean will be triggered on and you will not be able to add another identical component untill the previous one is removed.
    Does this help?
     
  3. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    You can achieve this easily using a list!
    I've created a little status system for you below.

    It will add status effects (with a time) to a list, count down their times, and remove any that reach 0.

    The actual status system, put this on an object and press 1, 2, or 3 in game to test it
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class StatusSystem : MonoBehaviour {
    6.  
    7.     //List of effects
    8.     List<StatusEffect> statuses;
    9.  
    10.     void Start () {
    11.         statuses = new List<StatusEffect>();
    12.     }
    13.  
    14.     void Update () {
    15.         //Add status effect, press 1, 2, or 3
    16.         if(Input.GetKeyDown(KeyCode.Alpha1)){
    17.             statuses.Add(new StatusEffect("AttackUp", 2));
    18.             Debug.Log(statuses[statuses.Count-1].name+" added!");
    19.         }
    20.         if(Input.GetKeyDown(KeyCode.Alpha2)){
    21.             statuses.Add(new StatusEffect("SpeedDown", 2));
    22.             Debug.Log(statuses[statuses.Count-1].name+" added!");
    23.         }
    24.         if(Input.GetKeyDown(KeyCode.Alpha3)){
    25.             statuses.Add(new StatusEffect("Poison", 2));
    26.             Debug.Log(statuses[statuses.Count-1].name+" added!");
    27.         }
    28.  
    29.         //Lower status time
    30.         for(int i=0; i<statuses.Count; i++){
    31.             statuses[i].time -= Time.deltaTime;
    32.             if(statuses[i].time <= 0){
    33.                 //Remove status and do any needed removal code
    34.                 Debug.Log(statuses[i].name+" ended! ("+(statuses.Count-1)+" remaining)");
    35.                 statuses.RemoveAt(i);
    36.             }
    37.         }
    38.     }
    39. }
    40.  
    The status effect only holds it's name and time length (Do not add this to any objects, it's added into the list above when needed)
    Code (CSharp):
    1. //Basic empty class, only holds 2 properties.
    2. //It does not extend MonoBehaviour, and so it doesn't have any built in functions like Awake or Update
    3. public class StatusEffect {
    4.  
    5.     public float time;
    6.     public string name;
    7.  
    8.     public StatusEffect (string _name, float _time) {
    9.         name = _name;
    10.         time = _time;
    11.     }
    12. }
    13.  
    Goodluck!
     
  4. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    It doesn't "help" per-say. I already know how to add components, so that wasn't the issue. The issue is that if I'm adding each effect as a script, then I'll have dozens of Updates running on each character (since each status script will be updating its timer countdown).

    I completely thought about using a list, but I DIDN'T think about using a loop to countdown their timers. :oops: Thanks!