Search Unity

Poison Debuff using Coroutines, Some Questions.

Discussion in 'Scripting' started by Gallantl33, Mar 5, 2015.

  1. Gallantl33

    Gallantl33

    Joined:
    Mar 2, 2015
    Posts:
    12
    Hey guys/gals,
    I wrote a function using coroutines for the first time, and i have a question about the bool that I pass into the functions that follow.
    I originally included it so the coroutine would run only once, and not apply infinite poison. The logic is that the couroutine turns the bool off so that it doesn't repeat itself indefinitely. Is this a good implementation??

    The main question I have is about the necessity of that bool I added in. I will be calling the PlayerPoisoned function upon a successful application of poision from a monster, not in the update function.

    Code (CSharp):
    1. //PoisonDegen. If the monster successfully applies poison, it calls the void function, and passes is parameters, which runs the coroutine
    2.     IEnumerator PoisonDegen(int degenPoison, int degenTime, bool isPoisoned)  {
    3.      
    4.         for ( int degenCounter = degenTime ; degenCounter >0; degenCounter--)
    5.         {
    6.             if (degenCounter != 0) {
    7.                 //display the GUI element
    8.                 playerVitals = gameObject.GetComponent<PlayerVitals> ();
    9.                 playerVitals.playerCurHealth -= degenPoison;
    10.                 yield return new WaitForSeconds(1.0f);
    11.             }
    12.             else
    13.             {isPoisoned=false;
    14.             }
    15.         }
    16.     }
    17.     public void PlayerPoison( int degenPoison,int degenTime, bool isPoisoned)
    18.     {
    19.      
    20.         if (isPoisoned == true) {
    21.             StartCoroutine (PoisonDegen(degenPoison, degenTime,isPoisoned));
    22.         } else
    23.             return;
    24.     }
    Sorry if this is confusing!
    Thanks
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I don't see any need for it. It doesn't actually stop the coroutine.

    And the bool to start it is a whole new bool, it's not like 'isPoisned' is stateful to the scope of the object.
     
  3. Gallantl33

    Gallantl33

    Joined:
    Mar 2, 2015
    Posts:
    12
    You're absolutely right, my logic was kind of muddled there on the method of calling the function. I had it in my head that it would be called every frame but it clearly won't if I am calling it only the moment the player is poisoned.

    Thanks for straightening me out
     
  4. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    I don't see anything wrong with it. Looks pretty nice. More sophisticated than I would have written the first few tries but that's good.

    I think when you call PlayerPoison you want to set isPoisoned to true. Right now you're checking whether it's true, but if you're only calling it when you get poisoned then why would you check it? Plus you never set it anywhere else.

    So I'd change it to be two lines in PlayerPoison, the startCoroutine and isPoisoned = true, nothing else for now.

    Now what you can do with isPoisoned is check if the person is poisoned when other monsters hit with some kind of attack that does more damage to a poisoned player... you can cure the poison (a simple isPoisoned = false

    If you add cures.. well in PoisonDegen you can say else { isPoisoned = false; degenCounter = 0} and that will end the poison.

    Hope that helps.
     
  5. Gallantl33

    Gallantl33

    Joined:
    Mar 2, 2015
    Posts:
    12
    Originally that was why I wanted that code in there.... I think. if I set the void call to set a local variable isPoisoned to be true, and then stipulated in the coroutine that if isPoisoned becomes false, stop the coroutine, that would be a valid piece of code? Mainly just trying to figure out the best way to scope the bool so it's easy and efficient to edit and stop the coroutine immediately

    I think you guys just got my brain in gear. Sometime you just get stuck in an illogical pathway