BuffController - jimdroberts/FishMMO GitHub Wiki

Description

Controls the application, ticking, and removal of buffs for a character, including network synchronization. Manages buff state, stacking, and event handling in the FishMMO system.


API Access

Fields

  • private Dictionary<int, Buff> buffs

    Internal dictionary mapping buff template IDs to active buff instances.

  • private List keysToRemove

    Temporary list of keys to remove after update loop (avoids modifying dictionary during iteration).

Properties

  • public Dictionary<int, Buff> Buffs { get; }

    Public accessor for the character's active buffs.

Methods

  • public override void ReadPayload(NetworkConnection conn, Reader reader)

    Reads the buff state from the network payload and applies each buff to the character. conn (NetworkConnection): The network connection. reader (Reader): The network reader to read from.

  • public override void WritePayload(NetworkConnection conn, Writer writer)

    Writes the current buff state to the network payload for synchronization. conn (NetworkConnection): The network connection. writer (Writer): The network writer to write to.

  • void Update()

    Unity Update callback. Handles ticking, expiration, and removal of buffs each frame.

  • public void Apply(BaseBuffTemplate template)

    Applies a buff to the character by template, creating a new instance if needed and handling stacking. template (BaseBuffTemplate): The buff template to apply.

  • public void Apply(Buff buff)

    Applies a buff instance to the character if not already present, invoking appropriate events. buff (Buff): The buff instance to apply.

  • public void Remove(int buffID)

    Removes a buff by template ID, invoking removal events and cleaning up. buffID (int): The template ID of the buff to remove.

  • public void RemoveRandom(System.Random rng, bool includeBuffs = false, bool includeDebuffs = false)

    Removes a random buff or debuff from the character, with options to include buffs and/or debuffs. rng (System.Random): The random number generator to use. includeBuffs (bool): Whether to include buffs in the selection. includeDebuffs (bool): Whether to include debuffs in the selection.

  • public void RemoveAll(bool ignoreInvokeRemove = false)

    Removes all non-permanent buffs from the character, optionally suppressing removal events. ignoreInvokeRemove (bool): If true, does not invoke OnRemoveBuff/OnRemoveDebuff events.

  • public override void ResetState(bool asServer)

    Resets the buff controller state, clearing all buffs. asServer (bool): Whether the reset is being performed on the server.

  • public override void OnStartCharacter()

    Called when the character is started on the client. Registers broadcast listeners for buff updates.

  • public override void OnStopCharacter()

    Called when the character is stopped on the client. Unregisters buff update listeners.


Basic Usage

Setup

  1. Attach the BuffController to the character GameObject.
  2. Ensure buff templates are registered and available in the system.
  3. Use Apply and Remove methods to manage buffs at runtime.
  4. Integrate with network payloads for multiplayer synchronization.

Example

// Example 1: Applying a buff by template
buffController.Apply(buffTemplate);

// Example 2: Removing a buff by ID
buffController.Remove(buffTemplate.ID);

Best Practices

  • Always use Apply and Remove methods to manage buffs for proper event handling.
  • Use keysToRemove to avoid modifying the buffs dictionary during iteration.
  • Register and unregister network broadcasts to prevent memory leaks.
  • Handle stacking logic according to the template's MaxStacks property.
⚠️ **GitHub.com Fallback** ⚠️