Quantcast
Viewing latest article 15
Browse Latest Browse All 85

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(); 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(); 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(); 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!

Viewing latest article 15
Browse Latest Browse All 85

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>