# 7. Ability Tests Balancing and Pitfalls Part of [[Ability System]]. Previous: [[6. Ability Bar UI and Input]]. > [!warning] Test status > The rules-engine assertions ran as a .NET 10 console program on 2026-12-01: **31/31 passed** (output below). The NUnit versions and the Editor validator were type-checked against 6000.5.8f1 but weren't run in the Test Runner. ## 7.1 EditMode tests Because the rules engine takes time as a parameter, tests are instant and deterministic. There's no `WaitForSeconds` and no scene. Put the four core classes in their own asmdef (`Abilities.Core`, with **No Engine References** ticked, which keeps them honest), and reference it from an Editor-only test asmdef ([[Unity Test Framework]]). ```csharp // Assets/Tests/EditMode/AbilityCoreTests.cs using NUnit.Framework; public class AbilityCoreTests { [Test] public void RegenWaitsForDelayThenResumes() { var mana = new ResourcePool("mana", 100f, 10f, 1f); mana.TrySpend(30f); mana.Tick(0.5f); Assert.AreEqual(70f, mana.Current, 1e-4f); mana.Tick(1.0f); Assert.AreEqual(75f, mana.Current, 1e-4f); } [Test] public void ChargesRechargeOneAtATimeAndChain() { var cd = new CooldownTracker(); cd.Register("dash", 4, 2); Assert.IsTrue(cd.TryConsume("dash", 0, 0)); Assert.IsTrue(cd.TryConsume("dash", 0.1, 0)); Assert.IsFalse(cd.TryConsume("dash", 0.2, 0)); Assert.AreEqual(0, cd.Charges("dash", 3.9)); Assert.AreEqual(1, cd.Charges("dash", 4.0)); Assert.AreEqual(2, cd.Charges("dash", 8.0)); } [Test] public void LongFrameCatchesUpTicks() { var st = new StatusContainer(); var ticks = 0; st.Ticked += _ => ticks++; st.Apply(new StatusDef("burn", 2f, 0.5f, StackPolicy.StackAndRefresh, 3, "debuff")); st.Tick(1.25f); Assert.AreEqual(2, ticks); st.Tick(0.75f); Assert.AreEqual(4, ticks); Assert.AreEqual(0, st.Active.Count); } [Test] public void StacksCapAndTagsCount() { var st = new StatusContainer(); var burn = new StatusDef("burn", 2f, 0.5f, StackPolicy.StackAndRefresh, 3, "burning"); for (var i = 0; i < 5; i++) st.Apply(burn); Assert.AreEqual(3, st.StacksOf("burn")); Assert.IsTrue(st.HasTag("burning")); st.Remove("burn"); Assert.IsFalse(st.HasTag("burning")); } [Test] public void SilenceInterruptsCastAndRefundsNothingSpent() { var statuses = new StatusContainer(); var caster = new AbilityCaster(statuses, 1.0); var mana = new ResourcePool("mana", 50f, 0f); caster.AddResource(mana); caster.Grant(new AbilitySpec("heal", "mana", 5f, 0, 1, 1.0, true, true, new[] { "silence" })); Assert.AreEqual(CastResult.Started, caster.TryActivate("heal", 0)); statuses.Apply(new StatusDef("silence", 2f, 0f, StackPolicy.Refresh, 1, "silence")); caster.Tick(0.5); Assert.IsFalse(caster.IsCasting); Assert.AreEqual(50f, mana.Current, 1e-4f); Assert.AreEqual(CastResult.Blocked, caster.TryActivate("heal", 0.6)); } [Test] public void OffGcdAbilityIgnoresGcd() { var caster = new AbilityCaster(new StatusContainer(), 1.0); caster.AddResource(new ResourcePool("mana", 100f, 0f)); caster.Grant(new AbilitySpec("bolt", "mana", 10f, 3)); caster.Grant(new AbilitySpec("blink", "mana", 10f, 8, 2, 0, false)); Assert.AreEqual(CastResult.Completed, caster.TryActivate("bolt", 0)); Assert.AreEqual(CastResult.Completed, caster.TryActivate("blink", 0.1)); } } ``` Console run (the same rules, more cases): ```text PASS spend 30 of 100 mana PASS can't overspend, nothing changes PASS regen delay: no regen in the first 0.5 s PASS regen resumes after the delay (0.5 s of 10/s) PASS regen clamps at max PASS 2 charges can be used back to back PASS third use fails with 0 charges PASS first charge returns at t=4 (recharge started at t=0) PASS second charge chains from t=4, back at t=8 PASS GCD 1 s blocks then frees PASS cooldown reduction of 2 s: 6-1-2 = 3 s left PASS radial progress halfway PASS stack-and-refresh caps at 3 stacks, one instance PASS 1.25 s of a 0.5 s DoT = 2 ticks (long frame is caught up) PASS expires at 2 s after its 4th tick PASS tag count drops to 0 on expiry PASS independent stacks cap at 2 instances PASS third application refreshed the oldest instead of adding PASS Ignore policy: reapply while active is ignored PASS cleanse by tag removes both slows PASS cast-time ability starts; cost not yet paid PASS can't cast while casting PASS cast bar 2/3 at t=1.0 PASS cast completes at t=1.5; cost paid then PASS fireball on cooldown PASS off-GCD instant ability works during GCD PASS heal starts after GCD (fireball committed at 1.5 -> GCD until 2.5) PASS silence interrupts heal; nothing spent PASS silenced: blocked PASS unknown ability PASS not enough mana ALL PASS ``` ## 7.2 Tag vocabulary validator Free-form tag strings are a typo waiting to happen. This Editor menu item checks every ability and status asset against one list: ```csharp // Assets/Editor/AbilityTagValidator.cs using System.Collections.Generic; using UnityEditor; using UnityEngine; public static class AbilityTagValidator { static readonly HashSet<string> Vocabulary = new HashSet<string> { "stun", "root", "silence", "slow", "debuff", "buff", "burning", "frozen", "unstoppable" }; [MenuItem("Tools/Abilities/Validate Tags")] public static void Validate() { var problems = 0; foreach (var guid in AssetDatabase.FindAssets("t:StatusEffectDefinition")) { var asset = AssetDatabase.LoadAssetAtPath<StatusEffectDefinition>(AssetDatabase.GUIDToAssetPath(guid)); foreach (var tag in asset.tags) problems += Report(asset, tag); } foreach (var guid in AssetDatabase.FindAssets("t:AbilityDefinition")) { var asset = AssetDatabase.LoadAssetAtPath<AbilityDefinition>(AssetDatabase.GUIDToAssetPath(guid)); foreach (var tag in asset.blockedByTags) problems += Report(asset, tag); if (asset.effects.Contains(null)) { Debug.LogWarning(quot;{asset.name}: has an empty effect slot.", asset); problems++; } } Debug.Log(problems == 0 ? "Ability tags OK." : quot;Ability tags: {problems} problem(s)."); } static int Report(Object asset, string tag) { if (Vocabulary.Contains(tag)) return 0; Debug.LogWarning(quot;{asset.name}: unknown tag '{tag}'.", asset); return 1; } } ``` Run it from CI with `-executeMethod AbilityTagValidator.Validate` ([[Build Automation and CI]]). ## 7.3 A balance sheet Put every ability in one table, and compute **sustained** damage and mana per second. That's the number players feel over a fight: | Ability | Dmg | DoT (total at max stacks) | Cast | CD | Cost | Sustained DPS ≈ (Dmg + DoT) / max(CD, Cast, GCD) | Mana/s ≈ Cost / max(CD, Cast, GCD) | |---|---|---|---|---|---|---|---| | Fireball | 25 | 5 × 4 ticks = 20 (see note) | 1.5 | 5 | 20 | 9 | 4.0 | | Strike | 12 | — | 0 | 0.6 | 0 | 20 | 0 | | Frost Nova | 10 | — | 0 | 12 | 30 | 0.8 (+ control) | 2.5 | The sheet catches a design bug straight away. *Burning* stacks to 3, but one player can never reach 3 stacks: Fireball's 5 s cooldown is longer than the 4 s burn, so each stack expires before the next lands. Either the stacks are meant for *several* casters (fine, write that down), or the burn should last more than 10 s. Compare total mana per second against **regen** (5/s here). If the player's best rotation costs 9 mana/s against 5/s regen, they lose 4 mana/s net, so 100 mana lasts about 25 s. Is that the length of a typical fight? That one comparison is most of resource balance. Keep the sheet as CSV and import it as in [[4. Economy Tuning Sheets and CSV Import]], so the numbers live in one place. ## 7.4 Multiplayer notes - **The server owns the rules.** Clients send "pressed ability 2 aiming at X". The server runs `Check`/`TryActivate` and replicates the outcome. Clients may *predict* the cast bar and cooldown for responsiveness, and correct themselves on rejection. - The core takes `double now`, so the server's clock drives it. Send cooldown **end timestamps** in server time, not "seconds remaining". - See [[Multiplayer with Netcode for GameObjects]] for RPC and NetworkVariable patterns. ## 7.5 Pitfalls | Symptom | Cause | Fix | |---|---|---| | Mana spent but nothing happened | Cost committed, then targeting failed | Resolve targeting before activating (`AbilityUser.TryActivate`) | | Spell fired for free after a mana drain | No cost re-check at completion | `AbilityCaster.Tick` re-checks | | Second press during a cast hit a different target | Target overwritten by a refused press | Check first, then assign the pending target | | DoT ticks fewer times on slow devices | One tick per frame at most | Catch-up `while` loop (part 3) | | Two stuns overlapping: the first ending frees the player | Boolean "stunned" flag | Tag **counts** | | Cooldowns keep running while paused | Unscaled or real-time clock | `Time.timeAsDouble` (scaled), or a game clock you control | | Charges come back late if the UI isn't open | Recharge computed from "when checked" | Chain from `NextChargeAt` (part 2) | | Icon shows a full cooldown but the ability works | Mask shows recharge while charges remain | Show only the GCD when charges > 0 (part 6) | | "Stun" doesn't block anything | Tag typo or case | Fixed vocabulary + validator (7.2) | | Effect assets change between abilities unexpectedly | Effect holds per-use state in a field | Effects must be stateless; put state in the context or on `AbilityUser` | | Handlers fire twice after respawn | Input `performed` subscribed in `OnEnable` without unsubscribing | Store and remove handlers (part 6) |