CharacterAttributeController - jimdroberts/FishMMO GitHub Wiki

Description

Controls and manages all character attributes and resource attributes for a character, including initialization, synchronization, and regeneration. Handles attribute relationships, network payloads, and event-driven updates in the FishMMO system.


API Access

Fields

  • public CharacterAttributeTemplateDatabase CharacterAttributeDatabase

    Reference to the ScriptableObject database containing all character attribute templates. Used to initialize and manage available attributes for this character.

  • public CharacterAttributeTemplate HealthResourceTemplate

    Template for the health resource attribute (e.g., HP).

  • public CharacterAttributeTemplate HealthRegenerationTemplate

    Template for the health regeneration attribute.

  • public CharacterAttributeTemplate ManaResourceTemplate

    Template for the mana resource attribute (e.g., MP).

  • public CharacterAttributeTemplate ManaRegenerationTemplate

    Template for the mana regeneration attribute.

  • public CharacterAttributeTemplate StaminaResourceTemplate

    Template for the stamina resource attribute.

  • public CharacterAttributeTemplate StaminaRegenerationTemplate

    Template for the stamina regeneration attribute.

  • private readonly Dictionary<int, CharacterAttribute> attributes

    Dictionary of all non-resource character attributes, keyed by template ID.

  • private readonly Dictionary<int, CharacterResourceAttribute> resourceAttributes

    Dictionary of all resource character attributes (e.g., health, mana), keyed by template ID.

Properties

  • public Dictionary<int, CharacterAttribute> Attributes { get; }

    Public accessor for all non-resource character attributes.

  • public Dictionary<int, CharacterResourceAttribute> ResourceAttributes { get; }

    Public accessor for all resource character attributes.

Methods

  • public override void OnAwake()

    Unity lifecycle method called when the component is initialized. Initializes all attributes and resource attributes from the database, and sets up dependencies.

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

    Reads attribute and resource attribute state from the network payload and applies 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 attribute and resource attribute state to the network payload for synchronization. conn (NetworkConnection): The network connection. writer (Writer): The network writer to write to.

  • public override void ResetState(bool asServer)

    Resets the attribute controller state, restoring resource attributes to their final values. asServer (bool): Whether the reset is being performed on the server.

  • public void SetAttribute(int id, int value)

    Sets the value of a non-resource attribute by template ID. id (int): The template ID. value (int): The value to set.

  • public void SetResourceAttribute(int id, int value, float currentValue)

    Sets the value and current value of a resource attribute by template ID. id (int): The template ID. value (int): The value to set. currentValue (float): The current value to set.

  • public bool TryGetAttribute(CharacterAttributeTemplate template, out CharacterAttribute attribute)

    Tries to get a non-resource attribute by template. template (CharacterAttributeTemplate): The template to look up. attribute (CharacterAttribute): The resulting attribute if found. Returns true if found, false otherwise (bool).

  • public bool TryGetAttribute(int id, out CharacterAttribute attribute)

    Tries to get a non-resource attribute by template ID. id (int): The template ID. attribute (CharacterAttribute): The resulting attribute if found. Returns true if found, false otherwise (bool).

  • public bool TryGetResourceAttribute(CharacterAttributeTemplate template, out CharacterResourceAttribute attribute)

    Tries to get a resource attribute by template. template (CharacterAttributeTemplate): The template to look up. attribute (CharacterResourceAttribute): The resulting attribute if found. Returns true if found, false otherwise (bool).

  • public float GetHealthResourceAttributeCurrentPercentage()

    Gets the current health resource attribute as a percentage. Returns the percentage as a float.

  • public float GetManaResourceAttributeCurrentPercentage()

    Gets the current mana resource attribute as a percentage. Returns the percentage as a float.

  • public float GetStaminaResourceAttributeCurrentPercentage()

    Gets the current stamina resource attribute as a percentage. Returns the percentage as a float.

  • public bool TryGetHealthAttribute(out CharacterResourceAttribute health)

    Tries to get the health resource attribute. health (CharacterResourceAttribute): The resulting attribute if found. Returns true if found, false otherwise (bool).

  • public bool TryGetManaAttribute(out CharacterResourceAttribute mana)

    Tries to get the mana resource attribute. mana (CharacterResourceAttribute): The resulting attribute if found. Returns true if found, false otherwise (bool).

  • public bool TryGetStaminaAttribute(out CharacterResourceAttribute stamina)

    Tries to get the stamina resource attribute. stamina (CharacterResourceAttribute): The resulting attribute if found. Returns true if found, false otherwise (bool).

  • public bool TryGetResourceAttribute(int id, out CharacterResourceAttribute attribute)

    Tries to get a resource attribute by template ID. id (int): The template ID. attribute (CharacterResourceAttribute): The resulting attribute if found. Returns true if found, false otherwise (bool).

  • public void AddAttribute(CharacterAttribute instance)

    Adds a non-resource attribute instance to the controller. instance (CharacterAttribute): The attribute instance to add.

  • public void InitializeAttributeDependents()

    Initializes parent/child/dependant relationships for all non-resource attributes.

  • public void AddResourceAttribute(CharacterResourceAttribute instance)

    Adds a resource attribute instance to the controller. instance (CharacterResourceAttribute): The resource attribute instance to add.

  • public void InitializeResourceAttributeDependents()

    Initializes parent/child/dependant relationships for all resource attributes.

  • public void Regenerate(float deltaTime)

    Handles periodic regeneration of health, mana, and stamina based on regeneration attributes. deltaTime (float): The time since the last update.

  • public void ApplyResourceState(CharacterAttributeResourceState resourceState)

    Applies a resource state to the controller, updating health, mana, and stamina values. resourceState (CharacterAttributeResourceState): The resource state to apply.

  • public CharacterAttributeResourceState GetResourceState()

    Gets the current resource state for health, mana, and stamina. Returns a CharacterAttributeResourceState struct.


Basic Usage

Setup

  1. Assign a CharacterAttributeTemplateDatabase to the controller in the Inspector.
  2. Set up resource and regeneration templates for health, mana, and stamina.
  3. Use AddAttribute and AddResourceAttribute to add attributes as needed.
  4. Use Regenerate to handle periodic regeneration.

Example

// Example 1: Initializing attributes on awake
characterAttributeController.OnAwake();

// Example 2: Regenerating resources each frame
characterAttributeController.Regenerate(Time.deltaTime);

Best Practices

  • Always initialize attribute and resource relationships after adding attributes.
  • Use TryGet methods to safely access attributes by template or ID.
  • Use network payload methods for multiplayer synchronization.
  • Keep attribute templates and relationships organized for maintainability.
⚠️ **GitHub.com Fallback** ⚠️