Buff System
Short description: The Buff system is a data-driven, template-based framework for applying temporary (or permanent) effects to FishMMO characters.
Table of Contents
- Overview
- Supported Platforms
- Features / Capabilities / Security Features
- Prerequisites
- Installation / Build
- Quick Start Guides
- Configuration
- Usage Examples
- Operational Checks
- Flow Diagram
- Project Structure
- Block and Deflect
- License
Overview
The Buff system is a data-driven, template-based framework for applying temporary (or permanent) effects to FishMMO characters. It supports tick-based expiration, tick-based periodic effects, stacking, attribute modification, FX instantiation, and FishNet network synchronization with deterministic prediction via IPredictableController (Order=85). Buffs and debuffs share the same pipeline, distinguished only by an IsDebuff flag on the template.
Note: See the Detailed File-Level Topology in the parent
Prediction/README.md(../README.md#detailed-file-level-topology) for a file-level call/serialization topology and per-file interactions.
Supported Platforms
| Platform | Status | Notes |
|---|---|---|
| Windows | ✅ Supported | Primary development platform |
| Linux | ✅ Supported | Server and client builds |
| WebGL | ✅ Supported | Via Unity WebGL export |
Built with Unity 6.6 (6000.6.8f1) using IL2CPP scripting backend.
Features / Capabilities / Security Features
Features
- Tick-based expiration — Buffs expire deterministically using absolute network ticks (
ExpiryTick) - Tick-based periodic effects — Periodic
OnTickcallbacks at configurable intervals usingNextTickTick - ECA-driven periodic effects —
OnTickEventsfiresBuffTickEventtriggers each tick, so a DoT is anApplyDamageActionand a HoT anApplyHealAction— no bespoke template subclass - DoTs behave like hits — health ticks route through
CharacterDamageController.Damage/.Heal, so they mitigate, generate threat, credit the caster and can kill - Caster attribution — the applying character is snapshotted on the
Buffand used as the attacker for every subsequent tick - Stacking — Buffs support up to
MaxStacks. Each hook states the whole contribution for the resulting stack count rather than adding a delta, so apply and remove are exact inverses at every count by construction - Attribute modification —
AttributeBuffTemplatestates bonus attributes through the attributed ledger, underModifierSource.Buff(templateID, entryIndex) - FX instantiation — Client-side visual effect prefabs attached to character mesh
- FishNet prediction support —
BuffControllerimplementsIPredictableController(Order=85) with Replicate/Reconcile viaBuffReconcileEntry[] - Permanent buffs —
IsPermanentflag keeps buffs from expiring and protects them from mass-removal operations - Buff/debuff distinction — Unified pipeline with
IsDebuffflag for categorization, events, and UI - Seven template types —
AttributeBuffTemplate,AttributeTickBuffTemplate,CompositeBuffTemplate,ResourceTickBuffTemplate,StateBuffTemplate,DamageNegationBuffTemplate,DeflectBuffTemplate - Spendable charges —
BaseBuffTemplate.InitialChargesandBuff.RemainingCharges, in whatever unit the template means (damage points for an absorb shield, deflections for a guard); reconciled, refilled on re-apply - Player dismissal — clicking a buff icon on your own HUD strip removes the whole thing, stacks and all, through a server-validated request. Debuffs are excluded by construction (
CanBeDismissedByPlayer => !IsDebuff), so the debuff strip has no click path at all. See Dismissal - Observer buff strips with real timers —
ObservedBuffEntrycarries stacks and seconds remaining in a seven-byte wire form overCharacterBuffsBroadcast, delta or full set - Static events —
OnAddBuff,OnRemoveBuff,OnAddDebuff,OnRemoveDebuff,OnBuffTickfor UI and other systems - Database persistence — a character's buff set is saved whole with its character row (
CharacterData.Buffs, in the row's transaction and under its version) and restored at login throughApply(Buff). Permanent buffs are neither saved nor restored. See Persistence
Security Features
- Buff state is server-authoritative — clients cannot apply or remove buffs without server validation
- Prediction reconcile restores authoritative buff state on mismatch, preventing client-side buff manipulation
- Dismissal is a client request, never an instruction.
DismissBuffBroadcastcarries a template ID and nothing else; the server resolves the sender's own character from the connection, refuses any templateCanBeDismissedByPlayerrejects, and is rate-limited on the existing per-connection ingress guard (operation byte 8, 100 ms debounce). A hand-crafted packet asking to clear a debuff, or another character's buff, changes nothing.
Prerequisites
- Unity 6.6 (6000.6.8f1)
- FishNetworking (FishNet)
- FishMMO Shared Core
Installation / Build
This system is an integrated module of the FishMMO Unity project. No separate installation is required. It is automatically included when the project is opened in Unity.
Quick Start Guides
Applying a buff from gameplay (ability, item, region):
// Get the target's BuffController
IBuffController buffController = target.GetComponent<BuffController>();
// The PREDICTED entry point. The tick must already be in the replicate-input domain, which is
// why it is a PredictionTick and not a raw uint — take it from the trigger's TickEventData.
buffController.Apply(myBuffTemplate, tickEventData.Tick, caster);
// The AUTHORITATIVE entry point, for server code holding a server tick. It maps the tick into
// the character's replicate domain through ResolveAuthoritativeTick first.
buffController.ApplyAuthoritative(myBuffTemplate, serverTick, caster);
The two entry points are not interchangeable. Apply marks the application as predicted by this
peer; on a peer that does not simulate this character's buffs that entry is provisional and is
dropped unless the server names it (see Predicted cross-character buffs).
ApplyAuthoritative confirms.
Removing a buff:
// Remove a specific buff by template ID
buffController.Remove(buffTemplateID);
// Remove all non-permanent buffs
buffController.RemoveAll(ignoreInvokeRemove: false, includePermanent: false, preserveFX: false);
// Remove a random buff (with inclusion flags)
buffController.RemoveRandom(rng, includeBuffs: true, includeDebuffs: true);
RemoveAll(ignoreInvokeRemove, includePermanent, preserveFX) iterates a snapshot copy; it skips
IsPermanent buffs unless includePermanent is set, and preserveFX keeps the FX instances alive
across a removal that is going to be immediately re-populated (a model reload, a reconcile restore).
RemoveRandom(rng, includeBuffs, includeDebuffs) attempts up to 10 random selections, skipping permanent buffs and checking buff/debuff inclusion flags.
Creating a new buff template type:
- Create a new class extending
BaseBuffTemplateinTemplate/Types/. - Add a
[CreateAssetMenu]attribute for Unity's asset creation menu. - Implement the two abstract methods:
OnApply(Buff buff, ICharacter target)— Initial application effect.OnRemove(Buff buff, ICharacter target)— Cleanup when the buff is fully removed.
- Optionally override the three virtual hooks:
OnApplyStack(Buff buff, ICharacter target)— defaults toOnApply.OnRemoveStack(Buff buff, ICharacter target)— defaults toOnRemove.OnTick(Buff buff, ICharacter target)— defaults to firingOnTickEventsthroughInvokeTickEvents. A derived class that adds periodic behaviour must callbase.OnTick(buff, target), or the buff's authored tick events are silently dropped for that template type.InitialCharges— a virtual property (default 0) rather than a serialized field, so a template derives its charge pool from what it already authors.
- Optionally override
SecondaryTooltip(TooltipContent)for custom tooltip content. - Optionally override
OnApplyFX(Buff, ICharacter)(returns the instance) andOnRemoveFX(GameObject, ICharacter)for custom visual effects.OnApplyFXis also called with a null buff for an observed buff, because observers hold noBuffinstance for another character.
Important: Ensure OnApply/OnRemove and OnApplyStack/OnRemoveStack are symmetric — every effect applied must be fully reversed on removal to avoid modifier leaks.
Configuration
Template Properties
BaseBuffTemplate exposes the following configurable fields:
| Property | Type | Description |
|---|---|---|
FXPrefabReference |
AssetReferenceGameObject |
Addressable visual effect prefab instantiated on the character (client-side only) |
Description |
string |
Tooltip description text |
Icon |
Sprite |
UI icon (loaded at runtime from the serialized icon addressable reference) |
Duration |
float |
Total duration in seconds (0 = permanent or event-driven) |
TickRate |
float |
Interval in seconds between OnTick calls |
MaxStacks |
uint |
Maximum stack count (0 = no stacking) |
IsPermanent |
bool |
If true, buff does not expire and RemoveAll / RemoveRandom skip it |
IsDebuff |
bool |
Determines buff vs debuff categorization for events and UI |
OnTickEvents |
List<BuffTickEvent> |
ECA triggers fired on every tick. Initiator is the caster, target is the carrier. |
BaseBuffTemplate also exposes two derived properties. Neither is authored, and neither has a
field in the inspector — do not add one:
| Property | Value | Description |
|---|---|---|
InitialCharges |
virtual, default 0 | The template's own charge pool, expressed in whatever unit it already authors |
CanBeDismissedByPlayer |
!IsDebuff |
Whether the player may click this buff off their own HUD strip |
CanBeDismissedByPlayer is deliberately the only place the dismissal rule is written. The tooltip
hint, the client click path and the server handler each read it, so what the UI advertises and what
the server honours cannot drift apart. A debuff is undismissable because it is a debuff, not because
a panel forgot to wire a callback — see Dismissal.
Attribute Modification
AttributeBuffTemplate is one of seven concrete template types. It modifies character attributes and holds a List<BuffAttributeTemplate> BonusAttributes where each entry pairs a CharacterAttributeTemplate Template with an int Value.
Each hook states the whole contribution for the stack count that will be in effect once the
hook returns, through SetSource(ModifierSource.Buff(ID, entryIndex), Value * multiplier). It does
not add a delta.
| Hook | Multiplier written | Why that expression |
|---|---|---|
OnApply |
1 + Stacks |
The base application. Stacks is 0 on a fresh buff. |
OnApplyStack |
2 + Stacks |
Buff.AddStack raises the hook before it increments Stacks, so the count that will be in effect is Stacks + 1, plus the base application. |
OnRemoveStack |
Stacks |
Buff.RemoveStack raises the hook before it decrements, so the count that will remain is Stacks - 1, plus the base application. |
OnRemove |
— | ClearSourceGroup(ModifierSourceKind.Buff, ID) releases every entry this buff wrote, whatever index it used. |
OnTick |
— | No-op for attribute buffs. |
The off-by-one in OnApplyStack and OnRemoveStack is load-bearing and nothing in the type system
holds it, so AttributeStackLedgerTests walks every stack count in both directions.
The entryIndex is the entry's position in BonusAttributes. Two authored entries may name the
same character attribute — a flat part and a scalar part — and a single key per buff would silently
keep only the last of them.
Stating rather than adding is what makes the model safe: a reconcile that restores a buff at a
different stack count simply calls AddStack/RemoveStack until the counts match, and the final
call is the only one whose value survives.
All Template Types
| Template | Description |
|---|---|
AttributeBuffTemplate |
States flat attribute bonuses through the ledger. No tick effect. |
AttributeTickBuffTemplate |
Grants attribute bonuses on apply, plus periodic attribute modification on each tick. |
CompositeBuffTemplate |
Composes multiple BaseBuffTemplate references, delegating all hooks to each child template. |
ResourceTickBuffTemplate |
Periodically ticks resource attributes (e.g., health/mana regen or drain over time). Health ticks route through the damage pipeline. |
StateBuffTemplate |
Applies a character state flag on apply, removes it on removal. No tick effect. |
DamageNegationBuffTemplate |
Absorbs, reduces or negates incoming damage, and may raise a ShieldVolume. See Block and Deflect. |
DeflectBuffTemplate |
Turns an incoming ability object away — rejected, not mitigated. See Block and Deflect. |
Damage and heal over time
There are two ways to author a periodic health effect, and they compose.
ECA tick events (preferred). Put a BuffTickEvent in OnTickEvents and hang an
ApplyDamageAction or ApplyHealAction on it. The event fires with the buff's carrier as the
target and the caster as the initiator, so the action behaves exactly as the same action does
on an ability's on-hit event. Conditions, target selectors and both condition branches all work,
which means "a poison that only ticks while its victim is moving" is a condition rather than a new
template type. Nothing new needs writing in C#, and the AI reads the actions directly rather than
inferring intent from numbers.
Serialized resource ticks. ResourceTickBuffTemplate.TickAttributes (and the same list on
CompositeBuffTemplate) still handles the flat case. A tick against the health resource is
routed through ICharacterDamageController.Damage / .Heal; a tick against any other resource
(mana, stamina) writes the resource directly, since there are no damage semantics to borrow.
DamageAttribute on the template selects the resistance the tick is mitigated by — leave it empty
for true damage.
Both paths run: a CompositeBuffTemplate can fire tick events and drain a resource.
Why health goes through the damage controller. Both tick templates previously called
CharacterResourceAttribute.AddToCurrentValue, which clamps the value and raises an attribute-changed notification and nothing else. That skipped every consequence of being hurt — no resistance, noImmortalcheck, no combat entry, and noOnDamagedevent, so a DoT generated no threat at all. Worst of all, nothing could die of it:Kill()is only ever reached from insideDamage(), so a DoT drained its victim to zero health and stopped, and the "already dead" early-out at the top ofDamage()then rejected every subsequent hit — leaving a character permanently alive at nothing.
Attribution
Buff snapshots the applying character (Buff.Caster) so a tick that fires seconds later still
has somebody to credit for threat, kill credit and combat state. It is passed by
ApplyBuffAction from the ECA initiator; sources with no initiator, such as region hazards, apply
with none.
The snapshot is deliberately not serialized into the reconcile payload or the database. The
server holds the authoritative buff for its whole life, which is the only place attribution is acted
on. If the caster is destroyed — disconnected, despawned — Buff.Caster returns null and the tick
still lands: a lingering poison is part of the simulation whether or not whoever cast it is still
in the scene. It simply credits nobody.
Prediction and replay
Ticks run on the client as well as the server, which is what keeps predicted health in step. The
authoritative consequences are already gated inside the damage controller — Kill() returns
immediately off the server. Reconcile replays every input since the last authoritative state, so a
tick fires again on each pass; Buff.IsReplaying is true during those, and is passed as
ignoreAchievements so a single tick of poison cannot count a dozen times toward an achievement.
Static Events
All events are defined on IBuffController:
| Event | Signature | When Fired |
|---|---|---|
OnBuffTick |
Action<IBuffController, Buff, uint> |
Each time a buff's OnTick fires during Tick() |
OnAddBuff |
Action<IBuffController, Buff> |
When a non-debuff is applied |
OnRemoveBuff |
Action<IBuffController, Buff> |
When a non-debuff is removed |
OnAddDebuff |
Action<IBuffController, Buff> |
When a debuff is applied |
OnRemoveDebuff |
Action<IBuffController, Buff> |
When a debuff is removed |
Every event carries the owning controller, and that is load-bearing. These are STATIC — one invocation list for the whole process — and they used to carry the Buff alone, which has no owner. Every buff applied to every NPC, pet, summon and other player in view was therefore delivered to the local player's own HUD strip and drawn on it. Keying the strip by template ID hid it: N mobs carrying one debuff collapsed into a single icon that looked plausible. A static event cannot be scoped at subscription time, so a subscriber must compare the controller against the character it is showing — see UITKBuffContainer.IsOwnController.
Dismissal
A player may click a buff off their own HUD strip. BaseBuffTemplate.CanBeDismissedByPlayer is the whole rule (!IsDebuff), and it is read in three places so they cannot disagree: the tooltip that advertises the click, the strip that makes it, and the server that decides.
- Client —
UITKBuffContainersendsDismissBuffBroadcast { TemplateID }onChannel.Reliable. Nothing is removed locally: the icon leaves when the server's reconcile arrives. - Server —
OnClientDismissBuffBroadcastReceivedresolves the SENDER'S OWN character and its own buff container, refuses anythingCanBeDismissedByPlayerrejects, then callsBuffController.Remove(templateID). A debuff cannot be cleared this way, however it was asked for. - No new sync path —
Removemarks the snapshot dirty, and the owner'sRestoreFromReconcilefiresOnRemoveBuffexactly as an expiring buff does.
External Integration Points
The buff system is consumed by and interacts with:
- Ability System — Abilities apply buffs/debuffs to targets through
ApplyBuffAction, which callsBuffController.Apply(template, PredictionTick, caster)with the tick from the trigger'sTickEventData. Server code holding a server tick usesApplyAuthoritative(template, serverTick, caster)instead. - Leash evade —
ApplyBuffActionrefuses every debuff another character tries to put on an evading NPC (CharacterEvade.RefusesBuff: stun, root, mesmerize, slow and every other debuff, because a slow is only a negative attribute modifier and the buff model has no narrower category). Asked after the stacks are drawn and before the loot-contribution record. Environmental debuffs (weather exposure, buff volumes) and an NPC's own debuffs on itself are unaffected, andImmortalstill takes debuffs as it always has. - CharacterAttribute System —
AttributeBuffTemplatestates attribute contributions through the attributed ledger (SetSource/ClearSourceGroup) on theExternalModifierlayer. - Death — the scene server's kill handler (
CharacterSystem) callsRemoveAll(false)on the dead character, clearing every non-permanent buff; the next save then drops their rows. - Item System — Items may apply buffs on use or equip.
- Database Layer — the buff set rides the character row as
CharacterBuffDataand is restored throughApply(Buff buff); see Persistence. - UI — Buff icons, tooltips, and timers are driven by the
OnAddBuff/OnRemoveBuff/OnAddDebuff/OnRemoveDebuff/OnBuffTickevents.
Notes
- FX Prefabs:
BaseBuffTemplate.OnApplyFXinstantiatesFXPrefabas a child of the character'sMeshRoot(orTransform) and returns it.BuffControllerowns the instance's lifetime: one per template (not per stack, not per re-application), destroyed throughOnRemoveFXwhen the buff expires, is dispelled, is removed by reconcile, or the character is torn down, and re-created fromIModelReadyHandler.OnModelReadyafter a model (re)load destroys it. FX prefabs may still self-destruct; they no longer have to. - Observer FX: an observer never simulates another character's buffs, so its FX is driven by the diff of the server-sent
ObservedBuffslist — added templates spawn, removed templates despawn. The owner drives FX from its own simulation instead, so it never spawns a second copy from its locally built observed list.
Usage Examples
Network Synchronization
Owner and observers are on two different paths, and they carry different information.
The owner: reconcile. Each authoritative tick BuffController writes its current set of
BuffReconcileEntry records into CharacterReconcileData.Buffs, and
CharacterReconcileDataDeltaSerializer ships only the entries that changed (index-delta with a
packed (deltaFlag | count) 16-bit header — see BuffReconcileEntry.WriteArrayDelta).
BuffController.OnReconcile calls RestoreFromReconcile(rd.Buffs, reconcileTick), which performs
an incremental Add/Remove patch against the local cached snapshot, then fires queued
OnAddBuff/OnAddDebuff/OnRemoveBuff/OnRemoveDebuff events after the patch loop completes
so nothing ever sees a half-restored set.
Observers: CharacterBuffsBroadcast. State forwarding is off (ObserverSyncMode), so an
observer never receives another character's replicate/reconcile stream and cannot be given
simulation state. It is sent a display-only strip of ObservedBuffEntry instead, scoped to the
character's observers and excluding the owner via
ObserverBroadcastScope.BroadcastToObserversExceptOwner, reliably — a dropped buff list leaves a
stale strip until the next change, with no self-correcting replacement. The message is a delta
(Buffs changed + Removed ids) or a full set (IsFullSet).
BroadcastObservedBuffs also applies the strip locally on the sender, with the full list
rather than the delta: a broadcast is never delivered back to its sender, and server-side code
reads ObservedBuffs as a complete list (PartySystem does).
The owner is excluded because it already holds the same state twice — as the reconcile array every
tick, and as the full simulation block of the spawn payload — and the observed strip is the
lossy copy, so the owner must not prefer it. RefreshObservedBuffsLocally fills the owner's
own ObservedBuffs from its own simulation for zero bytes, because the target frame reads that
list uniformly, including when the local player targets themselves.
Late joiners are served by WritePayload, not by an OnSpawnServer push. FishNet raises both from
one event, so sending a full strip on spawn and writing it into the payload sent the whole list
twice to every observer entering range. The payload copy is the one kept: it is length-framed,
ordered with the rest of the character's state, and cannot arrive before the object it describes.
LateJoinerReplayTests pins that pair.
ObservedBuffEntry (the observer's view)
| Field | Type | Wire |
|---|---|---|
TemplateID |
int |
sent |
Stacks |
int |
sent as a byte; counts above the base application, so 0 means one application and the cap is MaxStacks - 1 |
RemainingSeconds |
float |
sent as deciseconds in a ushort (MaxEncodableSeconds = 6553.5 s); 0 for a permanent buff |
TotalSeconds |
float |
not sent — resolved from BaseBuffTemplate.Duration on receipt, since it is authored content the receiver already holds |
Seven bytes on the wire. Duration travels as seconds remaining at send time, not as an absolute tick, because the receiving client's tick domain is its own; the observer counts down locally from receipt and drifts by the one-way latency, which is not worth a tick-domain translation for a bar a few pixels tall.
StructurallyEquals deliberately excludes RemainingSeconds — it moves every tick on every buff,
so including it would mark the whole list changed every tick and collapse the delta into a full
resend. The one case ordinary countdown cannot cover is a buff renewed since the observer was
last told, which would otherwise run out locally and be deleted while the character still holds it;
ObservedBuffWillLapse catches exactly that and answers with a full set.
ObservedBuffStatement carries the provenance of a strip — WholeStrip (every entry is the
server's word, and any template it does not name the server has stated the absence of) versus
Delta (only the changed entries and removed ids are stated; everything else in the merged strip
came out of the receiver's own container). Conflating those cost a defect on each side of the wire:
a delta confirmed predicted buffs the server had refused, and it disarmed ObservedBuffWillLapse
for the rest of a buff's life.
Predicted cross-character buffs
The caster's client tracks a buff on another character the moment its own ECA predicts it. If the server refuses that apply — the target died on the server's tick — no correction arrives on its own, because the observed-buff delta is computed against a server baseline that never contained the entry. A finite phantom at least ran out its local countdown; a permanent template's phantom icon and FX survived indefinitely.
So a predicted entry is provisional. Any server message that names the template confirms it;
one that goes unnamed past PredictedBuffConfirmationSeconds (3 s, recorded in this controller's
observer domain so no cross-domain tick comparison happens) is removed by the observer tick sweep,
the same way an observed buff that ran out locally is. The window errs long for the same reason
PredictedCombatEvents.ConfirmationWindowSeconds does: dropping a buff the server did apply merely
re-adds it on the next push, which flickers — worse than a phantom lingering a moment longer.
Spawn payload
WritePayload / ReadPayload are the FishNet spawn payload, not persistence. The payload opens
with the sender's current reference tick, then a block framed by its byte count and tagged with a
shape: the owner gets its simulation (per buff: template ID, ExpiryTick, NextTickTick,
Stacks, TickCount, CumulativeTickMultiplier, RemainingCharges), rebuilt into Buffs with
their ticks translated into the receiver's domain and applied through Apply(Buff buff); every
other connection gets the observed display list, built by the same method the broadcast uses.
ReadPayload checks the frame length against the bytes remaining and seeks to the frame's end
whatever it consumed, so a bad block cannot desynchronise the behaviours packed after it. Live
in-session sync is the reconcile (owner) and CharacterBuffsBroadcast (observers).
Persistence (DB)
A character's buffs are saved as one set with its character row, never on their own:
CharacterSystem.CaptureBuffSet converts each live buff's absolute ticks into remaining seconds and
next-tick seconds, with its stacks and tick count, into CharacterBuffData, and the list travels as
CharacterData.Buffs. The set replaces everything the database holds for the character, in the
same transaction as the row and only when the row is written, so an empty set is a real answer: the
rows of buffs that expired, were dismissed or were stripped on death are deleted. Every row carries
the row's version; a Buff has no persistence version of its own, because a per-instance counter
cannot order a set (a buff re-applied after it ended starts counting again). Remaining time is
frozen while the character is offline.
At login each row is rebuilt into a Buff against the loading server's tick and applied through
Apply(Buff buff).
Permanent buffs are neither saved nor restored (CharacterSystem.IsPersistedBuff). They are
the environment's — the weather-exposure buffs a WeatherExposureController or buff volume applies
and removes every tick — so they come back on their own within a tick of the login. A saved one had
no expiry to capture and was restored as a buff that expired on its first tick. Leaving them out of
the set deletes any rows saved before the rule at the next save, and the load skips any it still
finds until then.
Operational Checks
| Check | How to Verify | Expected Result |
|---|---|---|
| Buff applies correctly | Apply a buff template via BuffController.Apply(template, tickEventData.Tick, caster) |
Buff appears in controller dictionary; OnAddBuff/OnAddDebuff event fires |
| Stacking works | Apply same buff multiple times (up to MaxStacks) |
Stacks increments; attribute modifiers accumulate |
| Duration expiration | Wait for ExpiryTick to be reached |
Stacks decrement one at a time; buff removed when stacks reach 0 |
| Tick fires | Apply buff with non-zero TickRate |
OnTick called when NextTickTick is reached |
| Removal cleans up | Call Remove(buffID) |
All modifiers reversed; OnRemoveBuff/OnRemoveDebuff fires |
| Permanent buff protection | Call RemoveAll() with IsPermanent buff active |
Permanent buff remains |
| Network sync (owner) | Apply buff on server | Buff appears on owning client on the next reconcile via CharacterReconcileData.Buffs |
| Network sync (observer) | Apply buff on server with nearby observers | Buff appears on observers as an ObservedBuffEntry in CharacterBuffsBroadcast (delta, or full set when ObservedBuffWillLapse) — never through the reconcile |
| Predicted cross-character buff | Predict a buff on another character the server then refuses | The entry is dropped ~3 s later by the observer tick sweep; a server message naming the template confirms it instead |
| DB persistence | Save character with active buffs, reload | Buffs restored through Apply(Buff buff) with correct stacks/time; a buff that expired or was dismissed before the save does not return; a permanent buff is not saved and is re-applied by its own system |
| FX instantiation | Apply buff with FXPrefab set |
FX prefab spawned as child of MeshRoot; self-destroys after effect |
| Modifier balance | Apply and fully remove a stacked buff | Net modifier change is zero (every +V paired with -V) |
| Prediction reconcile | Simulate prediction mismatch | RestoreFromReconcile(BuffReconcileEntry[]) corrects client buff state |
Flow Diagram
High-Level Overview
flowchart LR
Apply[Buff apply event] --> Sys[BuffSystem]
Sys -->|register on owner| Owner[Character / Entity]
Tick[Network tick] --> Sys
Sys -->|periodic effect| Owner
Server[Server snapshot] -->|reconcile| Sys
Sys -->|expire / cleanse| Owner
Buff Lifecycle
1. Application
A buff enters the system through one of two Apply overloads on BuffController:
| Overload | Entry Point | Use Case |
|---|---|---|
Apply(BaseBuffTemplate, PredictionTick, ICharacter caster = null) |
Gameplay trigger (ability, item, region), predicted | Creates a new Buff, calls buff.Apply(Character), handles stacking + FX. Marks the entry predicted-by-this-peer |
ApplyAuthoritative(BaseBuffTemplate, uint serverTick, ICharacter caster = null) |
Server code holding a server tick | Maps the tick into the character's replicate domain through ResolveAuthoritativeTick, then applies. Confirms any predicted entry for that template |
Apply(Buff buff, bool suppressFX = false) |
DB load (CharacterSystem) / owner spawn payload (ReadPayload) |
Receives pre-constructed Buff with existing Stacks, calls buff.Apply(Character) + re-applies stack modifiers without incrementing Stacks |
Application flow (Apply(BaseBuffTemplate, uint currentTick)):
Apply(template, currentTick)
├── Buff already exists?
│ └── No → Create Buff(template.ID)
│ → buff.Apply(Character) // Template.OnApply → SetSource(1 x V) per attribute
│ → Add to dictionary
│ → Fire OnAddBuff / OnAddDebuff
├── Stacking allowed? (MaxStacks > 0 && Stacks < MaxStacks)
│ └── Yes → buff.AddStack(Character) // OnApplyStack → SetSource((2 + Stacks) x V)
│ → ++Stacks
│ → ResetDuration
│ └── No → ResetDuration only
└── SpawnBuffFX(template, buff) // Client-side, one tracked instance per template
Restoration flow (Apply(Buff buff)) — for DB load / network sync:
Apply(buff)
├── Already tracked? → skip
└── Not tracked:
→ buff.Apply(Character) // Base application: OnApply → SetSource(1 x V)
→ Add to dictionary
→ Loop buff.Stacks times:
│ → Template.OnApplyStack(buff, Character) // Re-apply each stack's modifiers
│ (Stacks NOT incremented — already set from source)
→ Fire OnAddBuff / OnAddDebuff
2. Ticking
BuffController.Tick(uint currentTick) runs each prediction tick (driven by OnReplicate):
foreach buff:
if HasExpired(currentTick):
if Stacks > 0:
RemoveStack(Character) // OnRemoveStack → SetSource(Stacks x V)
--Stacks
ResetDuration(currentTick) // Continue with remaining stacks
else:
Queue for removal
else:
if TryTick(Character, currentTick): // NextTickTick reached?
Template.OnTick(buff, Character) // Fires OnTickEvents, then any template-specific effect
Fire IBuffController.OnBuffTick
Advance NextTickTick
Timing uses absolute network ticks (ExpiryTick, NextTickTick) for deterministic prediction. HasExpired() compares via signed cast: (int)(currentTick - ExpiryTick) >= 0.
3. Removal
Remove(buffID)
→ buff.Remove(Character) // OnRemove → ClearSourceGroup(Buff, ID)
→ Remove from dictionary
→ Fire OnRemoveBuff / OnRemoveDebuff
Stacking Model
Each buff can have up to Template.MaxStacks stacks. The ledger entry is restated at every
transition, so the contribution is always exactly (1 + Stacks) * Value — the base application
plus one multiple per stack:
| Action | Ledger entry after | Stacks after |
|---|---|---|
| Initial apply | 1 × V |
0 |
| Add 1st stack | 2 × V |
1 |
| Add 2nd stack | 3 × V |
2 |
| Duration expires (Stacks=2) | 2 × V, reset duration |
1 |
| Duration expires (Stacks=1) | 1 × V, reset duration |
0 |
| Duration expires (Stacks=0) | entry released | removed |
Because every write is absolute, the model is balanced by construction rather than by pairing: any
sequence of stack changes that ends at count N leaves the same value as any other, and a
mismatched pair cannot drift the sheet the way the previous additive AddModifier(±V) shape could.
Prediction Pipeline
BuffController implements IPredictableController (Order=85), running after KCCPlayer (80) and before CooldownController (90), CharacterAttributeController (95), and AbilityController (100) in the prediction pipeline.
| Method | Behaviour |
|---|---|
PopulateInput |
No-op (buffs have no input) |
OnReplicate |
Calls Tick(input.GetTick()) — deterministic buff simulation per tick |
OnCreateReconcile |
Writes CreateReconcileSnapshot() → BuffReconcileEntry[] array |
OnReconcile |
Calls RestoreFromReconcile(entries) to restore authoritative buff state |
BuffReconcileEntry
Each BuffReconcileEntry captures the minimum state needed to restore a buff:
| Field | Type | Description |
|---|---|---|
TemplateID |
int |
The buff template ID |
ExpiryTick |
uint |
Absolute network tick when the buff expires |
NextTickTick |
uint |
Absolute network tick for next periodic tick |
Stacks |
int |
Current stack count |
TickCount |
int |
Ticks remaining for periodic effects |
CumulativeTickMultiplier |
int |
Running sum of (1 + Stacks) over every tick that has fired, so rollback replay and post-reconcile removal reverse exactly the cumulative modifier applied |
RemainingCharges |
int |
What is left of a spendable buff. Reconciled because the two peers move it independently: the server spends it as hits land, the owner refills it every time a channelled block re-applies. Left out, the owner's shield read full for the whole channel while the server's was already empty |
Array delta serialization uses index-based compression: unchanged entries are skipped, only modified/added/removed entries are transmitted.
Defensive guard (2025 audit): Both
Buffconstructors coerce a non-positivetickDeltaparameter to1f / 30fbefore computingExpiryTick/NextTickTick. This prevents aBuffconstructed beforeTimeManager.TickDeltais populated from collapsingExpiryTickontoapplyTickand immediately expiring.SetTickDelta(...)then resets the cached delta to the session's authoritative value once the network is fully up. SeeAssets/FishMMO/Tests/UnitTests/Prediction/BuffExpiryTests.cs::SetTickDelta_RepairsZeroInitializedTickDelta_RemainingSecondsCorrects.
Project Structure
Directory Structure
Buff/
├── Buff.cs # Runtime buff instance (tick-based timing, stacks, charges, caster snapshot)
├── BuffController.cs # Per-entity controller (CharacterBehaviour, IBuffController, IPredictableController Order=85, IModelReadyHandler)
├── BuffReconcileEntry.cs # Owner reconcile entry + index-delta array serialization (WriteArrayDelta/ReadArrayDelta)
├── ObservedBuffEntry.cs # Display-only observer entry (7-byte wire) + ObservedBuffStatement provenance
├── DamageMitigation.cs # The block/absorb/deflect resolution combat consults when a hit lands
├── ShieldVolume.cs # Authored shape (Sphere/Box/Capsule) tested in the character's OWN space
└── Template/
├── BaseBuffTemplate.cs # Abstract ScriptableObject base for all buff templates
├── BuffAttributeTemplate.cs # Serializable attribute+value pair for template configuration
├── BuffTemplateDatabase.cs # Name-to-template lookup database (ScriptableObject)
├── Events/
│ └── BuffTickEvent.cs # ECA trigger fired on each tick (DoT / HoT and anything periodic)
└── Types/
├── AttributeBuffTemplate.cs # Concrete template: states bonus attributes via SetSource
├── AttributeTickBuffTemplate.cs # Grants attributes on apply + periodic attribute modification on tick
├── CompositeBuffTemplate.cs # Composes multiple BaseBuffTemplate children, delegating all hooks
├── ResourceTickBuffTemplate.cs # Periodic resource attribute modification (regen/drain over time)
├── StateBuffTemplate.cs # Applies a character state flag on apply, removes on removal
├── DamageNegationBuffTemplate.cs # Block: Absorb / Reduce / Immune, plus an optional ShieldVolume
└── DeflectBuffTemplate.cs # Deflect: turns an incoming ability object away
Related Files (Outside This Directory)
Shared/Implementation/Entity/Prediction/Buff/BuffReconcileEntry.cs # Reconcile snapshot entry + index-delta array serialization
Shared/Implementation/Entity/Prediction/CharacterReconcileData.cs # Unified reconcile payload that carries Buffs[]
Shared/Implementation/Entity/Prediction/CharacterReconcileDataDeltaSerializer.cs # Delta writer that ships only changed buff entries
Shared/Implementation/Entity/BaseCharacter.cs # Client-side character cache used for observer routing
Inheritance Hierarchies
Runtime Instances
Buff # Standalone class (no inheritance)
Templates (ScriptableObjects)
CachedScriptableObject<BaseBuffTemplate>
└── BaseBuffTemplate # Abstract: Duration, TickRate, MaxStacks, IsPermanent, IsDebuff
├── AttributeBuffTemplate # Concrete: states BonusAttributes via SetSource
├── AttributeTickBuffTemplate # Concrete: Attributes on apply + periodic attribute ticks
├── CompositeBuffTemplate # Concrete: Composes multiple child templates
├── ResourceTickBuffTemplate # Concrete: Periodic resource modification (regen/drain)
├── StateBuffTemplate # Concrete: Applies/removes a character state flag
├── DamageNegationBuffTemplate # Concrete: Block — absorbs, reduces or negates incoming damage
└── DeflectBuffTemplate # Concrete: Deflect — turns an incoming ability object away
Controllers (NetworkBehaviour)
CharacterBehaviour
└── BuffController : IBuffController, IPredictableController (Order=85)
Configuration Types
BuffAttributeTemplate # [Serializable] class: Value + CharacterAttributeTemplate reference
Block and Deflect
Blocking and deflecting are buffs, not a subsystem. Nothing in the ability system knows what a
shield is: an ability applies a mitigation buff to its caster through the ordinary ECA
ApplyBuffAction, and combat consults that buff when a hit is being resolved. That means every
block and deflect variant is authored, not coded.
The two templates
| Template | What it does | When it is consulted |
|---|---|---|
DamageNegationBuffTemplate |
Takes damage off an incoming hit (Absorb / Reduce / Immune), and optionally raises a physical ShieldVolume that stops ability objects outright. |
CharacterDamageController.Damage for mitigation; AbilityObject.ApplyHit and AbilityApplyHitscanAction for the volume |
DeflectBuffTemplate |
Turns an incoming ability object away — the hit is rejected, not mitigated. | AbilityObject.ApplyHit, before the hit is accepted |
Both honour a facing arc, measured through TargetOrdering.IsWithinCone — the same test the cone
selector uses — so a shield held forward stops the sword in front and not the arrow in the back.
Set RequiresFacing false (or the angle to 360) for an omnidirectional barrier.
The shield volume
An arc says nothing about height or reach: a buckler and a tower shield cover identical cones, and a
shot at ankle height counts as "in front" of a shield held at the chest. DamageNegationBuffTemplate
therefore also carries a ShieldVolume — a real shape with real dimensions (Sphere, Box or
Capsule, plus LocalCenter, Radius/Size/Height).
An ability object whose impact lands inside the volume never touched the character: it is
destroyed outright and deals nothing, whatever Mode says. The arc and the mode still govern damage
that genuinely reaches you — melee, damage over time, splash — none of which has a meaningful impact
point on a shield. The two settings answer different questions on one template.
The volume is authored in the character's own space, and that is what makes it correct. Ability
hits are dispatched after the lag-compensation scope has closed, so a world-space volume read at
that moment would sit where the defender is now while the impact point came from where the defender
was — metres apart at 200 ms, blocking phantom hits and missing real ones. A local point against a
local volume has no such disagreement to have, because the body carried its own frame with it.
AbilitySweepHit.LocalPoint and LagCompensatedQuery.CompensatedHit.LocalPoint are captured inside
the scope for exactly this.
It gates both shapes of attack: a travelling projectile through AbilityObject.ApplyHit, and a
hitscan ray through AbilityApplyHitscanAction, where a covered victim stops the shot for everyone
behind them too.
Sweeping the shield outward (ShieldInterceptAction)
The gate above stops anything that would have struck you. It cannot stop what was going to miss — and a tower shield held out to the side should still sweep an arrow out of the air, and a fireball should die on the shield face rather than reaching your chest.
ShieldInterceptAction is the outward-looking half: wire it to the block ability's OnSpawn (a
channel re-spawns each tick, so that fires once per tick) and it overlaps the shield volume and
destroys the ability objects it catches. Leave its own Volume.Shape at None and it sweeps
whatever volumes the character's active block buffs already define, so the dimensions are authored
once, on the buff.
Two properties worth knowing:
- It cannot change whether damage lands. Both halves read the same authored volume, so anything it catches would have been gated anyway had it gone on to strike. It is an accelerator, not the mechanism.
- It runs after in-flight objects have already ticked. Every
AbilityObjectsubscribes toTimeManager.OnTickat spawn, so an object already in the air moves and resolves its own sweep first. A fast projectile can reach the body on the same tick this would have caught it — which is harmless precisely because the gate is what decides the outcome.
It may be predicted by the blocking client, unlike most hit resolution, because neither side of the test is interpolated: an ability object's position is a closed form every peer computes identically, and the blocker's own position is the one thing its client predicts and reconciles.
Both spend Buff.RemainingCharges, one counter whose unit belongs to the template: damage points
for an absorb shield, deflections for a guard. It is predicted state and rides
BuffReconcileEntry.RemainingCharges, because the two peers move it independently: the server spends
it as hits land, and the owner refills it every tick a channelled block re-applies its buff. It is deliberately not sent to observers —
how much more you have to hit through is not information the game otherwise gives.
Authoring a channelled shield (hold to block)
- Buff asset —
FishMMO/Character/Buff/Damage Negation Buff.Mode = Reduce(partial block) orImmune(full block) — this governs melee and splash.Amount= percent forReduce.Shield.Shape = Box,Size = (1.2, 1.4, 0.2),LocalCenter = (0, 1.1, 0.75)— the physical shield that stops projectiles.VolumeBlockCost = 0so holding the block does not wear it down.Duration≈ 2–3 ticks. Short on purpose: the channel re-applies it every tick, and it lapses shortly after the button is released.MaxStacks = 1.RequiresFacing = true,FacingAngleDegrees = 120.
- Ability event — an
AbilityOnSpawnEventwhose trigger hasTargetSelector = InitiatorTargetSelectorand oneApplyBuffActionnaming the buff above. - Ability template — give it the project's
ChanneledTemplateevent so it spawns one object per tick while held,AbilitySpawnTarget = Spawner(orPointBlank), and anAbilityObjectPrefabthat is the shield visual withLifeTimeof about one tick.ActivationTimemay be 0;ApplyChannelActivationFloorraises a held channel to at least one tick for you. - Optional — add a
ShieldInterceptActionto the sameOnSpawntrigger, leaving itsVolumeatNoneso it sweeps the buff's volume. This is what makes a projectile visibly die on the shield and lets a wide shield catch shots that would have gone past.
The shield exists for as long as the button is down because the buff is refreshed every tick, and
BuffController re-fills RemainingCharges on every re-apply — so a held block does not run out of
pool part-way through the channel.
Authoring a consumable barrier (absorbs N damage)
Same buff asset with Mode = Absorb, Amount = 500, and a real Duration. Apply it from any
ability, on-hit event or trigger through ApplyBuffAction. It disappears the moment the pool reaches
zero — DamageMitigation removes it through IBuffController.Remove, so the strip, the FX and the
observer push all hear about it exactly as they would for an expiry.
Authoring a deflect window (timed parry)
- Buff asset —
FishMMO/Character/Buff/Deflect Buff.DeflectAngleDegrees = 120.MaxDeflections = 0for a window that turns away everything arriving while it is up, or1for a single-use guard consumed by the first thing it stops.Duration= the parry window, a few ticks.
- Apply it exactly as above — an
AbilityOnSpawnEventwithInitiatorTargetSelectorandApplyBuffAction— from an instant ability whose object prefab plays the deflect animation.
A deflected object is never treated as having hit: no OnHit events, no hit count spent, no damage. It leaves along the incoming heading mirrored about the impact normal, and the defender is added to its hit set so the two cannot re-resolve against each other every tick.
How the three peers stay in step
- The server decides both, inside the rewind scope that resolved the hit.
- The caster's own client resolves its own hits, so it deflects and blocks identically — except
for an
Absorbpool, whose remainder it cannot see; it may over-predict damage there and the server's resource push corrects it on the next tick, the same bound every predicted number has. - Observers decide neither. A deflection reaches them as one bit on
AbilityObjectHitBroadcast, and they recompute the new heading from theNormalalready in that message — so the trajectory matches without the buff's timing having to.
Charges are spent only where IBuffController.SimulatesBuffEffects is true, so no peer drains a
pool it does not own.
License
This project is subject to the FishMMO project license.