AchievementController - jimdroberts/FishMMO GitHub Wiki

Description

Controls and tracks a character's achievements, including progress, tier, and event handling. Manages achievement state, updates, and event notifications for the FishMMO character system.


API Access

Fields

  • private Dictionary<int, Achievement> achievements

    Internal dictionary mapping achievement template IDs to achievement progress.

Properties

  • public Dictionary<int, Achievement> Achievements

    Public accessor for the character's achievements.

Methods

  • public override void ResetState(bool asServer)

    Resets the achievement state for this character, clearing all progress. 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 achievement updates.

  • public override void OnStopCharacter()

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

  • private void OnClientAchievementUpdateBroadcastReceived(AchievementUpdateBroadcast msg, Channel channel)

    Handles a broadcast from the server to update a single achievement's progress. msg (AchievementUpdateBroadcast): The achievement update message. channel (Channel): The network channel.

  • private void OnClientAchievementUpdateMultipleBroadcastReceived(AchievementUpdateMultipleBroadcast msg, Channel channel)

    Handles a broadcast from the server to update multiple achievements at once. msg (AchievementUpdateMultipleBroadcast): The multiple achievement update message. channel (Channel): The network channel.

  • public void SetAchievement(int templateID, byte tier, uint value, bool skipEvent = false)

    Sets or updates the progress for a specific achievement, optionally skipping the update event. templateID (int): The template ID of the achievement. tier (byte): The current tier to set. value (uint): The current value to set. skipEvent (bool): If true, does not invoke the update event.

  • public bool TryGetAchievement(int templateID, out Achievement achievement)

    Attempts to retrieve an achievement by template ID. templateID (int): The template ID of the achievement. achievement (Achievement): The resulting achievement if found. Returns true if the achievement exists, false otherwise (bool).

  • public void Increment(AchievementTemplate template, uint amount)

    Increments the progress of an achievement by a specified amount, handling tier advancement and rewards. template (AchievementTemplate): The achievement template to increment. amount (uint): The amount to increment the achievement's value by.


Basic Usage

Setup

  1. Ensure the character has an AchievementController attached (inherits from CharacterBehaviour).
  2. Register achievement templates and tiers in the system.
  3. Integrate achievement update broadcasts with the network client and server.
  4. Configure event listeners for OnUpdateAchievement and OnCompleteAchievement as needed.

Example

// Example 1: Incrementing an Achievement
// Assume 'controller' is an instance of AchievementController and 'template' is a valid AchievementTemplate.
controller.Increment(template, 10);

// Example 2: Setting an Achievement Directly
// Set achievement with template ID 1, tier 2, value 100, and trigger event.
controller.SetAchievement(1, 2, 100);

Best Practices

  • Always check for null templates before incrementing achievements.
  • Use event hooks (OnUpdateAchievement, OnCompleteAchievement) to handle UI updates and rewards.
  • Clear achievements on character reset to avoid stale data.
  • Register and unregister network broadcasts appropriately to prevent memory leaks or duplicate handlers.
⚠️ **GitHub.com Fallback** ⚠️