Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EvanesceExotica · Jul 05, 2016 at 06:01 AM · while-loopskipstatusspells

StatusEffect System - While Loop Being Skipped Over and Cancelled

I am building a spell system. Each character has a "Status" behaviour which holds each possible status effect. Keeping record of all of this is a Dictionary called "ourStatus". The int counts the number of times that particular effect has been applied. I have multiple ways this is implemented, one is through spells, and another through objects like campfires (if you walk into a campfire, it will apply the "dbuff_Burn" effect forever until you walk out of it.

Here is the "Apply Effects" Coroutine. I'm having trouble with what's in the outer if statement. Basically, for the spell system, it seems to work perfectly. Each effect lasts the proper duration and then disappears from the character's status.

However, when attempting to apply the burn effect from the campfire, the while loop below "while(Time.time < startTime + duration)" seems to jump immediately to the "ourStatus[effect]--" without waiting for the while loop to finish, causing it to terminate/break from the coroutine early, as ourStatus[effect] would be equal to zero. The while loop only applies one tick of damage before stopping, so it seems to be going through the loop at least once.

I don't understand why this is happening. When I remove the "ourStatus[effect]--", it works perfectly and provides the burn Damage Over Time like it should-- however, the effects from the spells, which don't have a duration of infinity, last forever (obviously, as the ourStatus[effect] is not being subtracted at the end of their duration.

   public IEnumerator ApplyEffects(StatusEffect.spellEffects effect, float dmg, float duration, float tickTime, GameObject source)
         {
            
     
             SetIcon(effect);
     
     
             float startTime = Time.time;
             if (effect == StatusEffect.spellEffects.dbuff_Burn || effect == StatusEffect.spellEffects.dbuff_Poison || effect == StatusEffect.spellEffects.buff_HealthRegen)
             {
                
                 while (Time.time < startTime + duration)
                 {
                   
                    
                     if (ourStatus[effect] <= 0)
                     {
                         yield break;
                     }
                     unit.ourHealth.adjustCurrentHealth(dmg, source, Health.DamageType.Environmental);
                     yield return new WaitForSeconds(tickTime);
     
                 }
                 if (ourStatus[effect] > 0)
                 {
                     ourStatus[effect]--;
                 }
     
             }
     
   //  ...
     }
 

I have two different ways that the "ApplyEffects" coroutine is being started. The first is using this method:

   public void EffectApplicationWrapper(StatusEffect.spellEffects effect, float dmg, float duration, float tickTime, GameObject source)
     {
         StartCoroutine(ApplyEffects(effect, dmg, duration, tickTime, source));
     }
 

The second is this way -- a snipped from one of my spell scripts:

  if (currentAbility.abilEffects.Count > 0)
                 {
                     Status theirStatus = hit.gameObject.GetComponent<Status>();
                     for (int i = 0; i < currentAbility.abilEffects.Count; i++)
                     {
                         StatusEffect mine = currentAbility.abilEffects[i];
                         theirStatus.ourStatus[mine.effect]++;
                         dt = new DelegateTestScript(hit.gameObject); 
                         dt.EffectApplicator(mine.damage, mine.duration, mine.effect, mine.tickTime, caster);
                     }
                 }

(Please forgive my terrible naming--I wrote those a long time ago.) Basically this way each effect has its own method --triggered by the DelegateTestScript.EffectApplicator method-- which calls the "ApplyEffects" coroutine directly. I don't want to post all of my scripts, as that would be too much text.

I tried switching the two ways, but that did not work. Here are the two methods from the script on the campfire:

    void OnTriggerExit2D(Collider2D hit)
     {
         Status status = hit.gameObject.GetComponent<Status>();
         if (status != null && alreadyEffected.Contains(hit.gameObject))
         {
             status.ourStatus[ourEffect]--;
             alreadyEffected.Remove(hit.gameObject);
             status.EffectApplicationWrapper(ourEffect, dmg, 5.0f, tickTime, this.gameObject);
         }
     }
 
     void OnTriggerStay2D(Collider2D hit)
     {
         if (!alreadyEffected.Contains(hit.gameObject))
         {
             Status status = hit.gameObject.GetComponent<Status>();
             if(status != null)
             {
                 status.ourStatus[ourEffect]++;
                 status.EffectApplicationWrapper(ourEffect, dmg, duration, tickTime, this.gameObject);
               
              }
                 alreadyEffected.Add(hit.gameObject);
             
         }
     }

I feel as if I'm missing something very simple. I don't understand why in one case, the while loop finishes fine, and in the other it jumps down to code below itself after one iteration. Let me know if I need to provide anymore code or information. Thank you in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[Closed]Unity crash, while/for loop 3 Answers

While loop out of control...maybe? I'm not sure. Trying to track Coroutines progress. 1 Answer

While loop crashes unity 1 Answer

is it safe to save info in PlayerPrefs? 2 Answers

Animation and FPS 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges