CharacterAttribute - jimdroberts/FishMMO GitHub Wiki

Description

Represents a character attribute, including its value, modifier, dependencies, and hierarchical relationships. Supports parent/child/dependency relationships and value propagation for complex attribute systems in the FishMMO system.


API Access

Fields

  • public CharacterAttributeTemplate Template { get; private set; }

    The template that defines this attribute's configuration and formulas.

  • private int value

    The base value of the attribute before any modifiers are applied.

  • private int modifier

    The sum of all modifiers applied to the attribute (e.g., from buffs, equipment, or formulas).

  • private int finalValue

    The final value of the attribute after applying modifiers and clamping (if enabled by the template).

  • private Dictionary<string, CharacterAttribute> parents

    Attributes that depend on this attribute (parents in the attribute hierarchy).

  • private Dictionary<string, CharacterAttribute> children

    Attributes that this attribute depends on (children in the attribute hierarchy).

  • private Dictionary<string, CharacterAttribute> dependencies

    Additional dependency attributes that may influence this attribute's value or logic.

  • public Action OnAttributeUpdated

    Event invoked when this attribute is updated (value, modifier, or final value changes).

Properties

  • public int Value { get; }

    Gets the base value of the attribute (before modifiers).

  • public int Modifier { get; }

    Gets the current modifier value.

  • public int FinalValue { get; }

    Gets the final value of the attribute after applying modifiers and clamping.

  • public float FinalValueAsFloat { get; }

    Returns the final value as a float.

  • public float FinalValueAsPct { get; }

    Returns the final value as a percentage (FinalValue * 0.01f).

  • public Dictionary<string, CharacterAttribute> Parents { get; }

    Gets the parent attributes (attributes that depend on this attribute).

  • public Dictionary<string, CharacterAttribute> Children { get; }

    Gets the child attributes (attributes this attribute depends on).

  • public Dictionary<string, CharacterAttribute> Dependencies { get; }

    Gets the dependency attributes (additional dependencies for this attribute).

Methods

  • public void SetValue(int newValue, bool forceUpdate = false)

    Sets the base value of the attribute and updates dependent values if changed. newValue (int): The new base value. forceUpdate (bool): If true, forces update even if value is unchanged.

  • public void AddValue(int amount, bool forceUpdate = false)

    Adds or subtracts an amount from the base value of the attribute. Addition: AddValue(123) | Subtraction: AddValue(-123) amount (int): The amount to add (can be negative). forceUpdate (bool): If true, forces update even if value is unchanged.

  • public void SetModifier(int newValue)

    Sets the modifier value and recalculates the final value if changed. newValue (int): The new modifier value.

  • public void AddModifier(int amount)

    Adds or subtracts an amount from the modifier and recalculates the final value if changed. amount (int): The amount to add (can be negative).

  • public void SetFinal(int newValue)

    Sets the final value directly. Use with caution; normally final value is calculated. newValue (int): The new final value.

  • public override string ToString()

    Returns a string representation of the attribute (name and final value).

  • public CharacterAttribute(int templateID, int initialValue, int initialModifier)

    Constructs a new CharacterAttribute from a template ID, initial value, and initial modifier. templateID (int): The template ID to use. initialValue (int): The initial base value. initialModifier (int): The initial modifier value.

  • public void AddParent(CharacterAttribute parent)

    Adds a parent attribute (an attribute that depends on this one). parent (CharacterAttribute): The parent attribute to add.

  • public void RemoveParent(CharacterAttribute parent)

    Removes a parent attribute. parent (CharacterAttribute): The parent attribute to remove.

  • public void AddChild(CharacterAttribute child)

    Adds a child attribute (an attribute this one depends on). child (CharacterAttribute): The child attribute to add.

  • public void RemoveChild(CharacterAttribute child)

    Removes a child attribute. child (CharacterAttribute): The child attribute to remove.

  • public void AddDependant(CharacterAttribute dependency)

    Adds a dependency attribute. dependency (CharacterAttribute): The dependency attribute to add.

  • public void RemoveDependant(CharacterAttribute dependency)

    Removes a dependency attribute. dependency (CharacterAttribute): The dependency attribute to remove.

  • public CharacterAttribute GetDependant(string name)

    Gets a dependency attribute by name. name (string): The name of the dependency attribute. Returns the dependency attribute, or null if not found (CharacterAttribute).

  • public int GetDependantValue(string name)

    Gets the value of a dependency attribute by name. name (string): The name of the dependency attribute. Returns the value of the dependency attribute, or 0 if not found (int).

  • public int GetDependantMinValue(string name)

    Gets the minimum value of a dependency attribute by name. name (string): The name of the dependency attribute. Returns the minimum value of the dependency attribute, or 0 if not found (int).

  • public int GetDependantMaxValue(string name)

    Gets the maximum value of a dependency attribute by name. name (string): The name of the dependency attribute. Returns the maximum value of the dependency attribute, or 0 if not found (int).

  • public int GetDependantModifier(string name)

    Gets the modifier of a dependency attribute by name. name (string): The name of the dependency attribute. Returns the modifier of the dependency attribute, or 0 if not found (int).

  • public int GetDependantFinalValue(string name)

    Gets the final value of a dependency attribute by name. name (string): The name of the dependency attribute. Returns the final value of the dependency attribute, or 0 if not found (int).

  • public void UpdateValues()

    Updates the attribute's values and propagates changes to parent attributes if needed.

  • public void UpdateValues(bool forceUpdate)

    Updates the attribute's values and propagates changes to parent attributes if needed. forceUpdate (bool): If true, forces update even if value is unchanged.


Basic Usage

Setup

  1. Create a CharacterAttribute instance using a valid template ID, initial value, and modifier.
  2. Set up parent, child, and dependency relationships as needed.
  3. Use SetValue, AddValue, SetModifier, and AddModifier to update attribute values.
  4. Use UpdateValues to propagate changes through the attribute hierarchy.

Example

// Example 1: Creating and updating an attribute
CharacterAttribute attr = new CharacterAttribute(templateID, 10, 0);
attr.AddValue(5);
attr.UpdateValues();

// Example 2: Setting up relationships
attr.AddChild(childAttr);
attr.AddParent(parentAttr);

Best Practices

  • Use parent, child, and dependency relationships to model complex attribute systems.
  • Always call UpdateValues after changing values to ensure propagation.
  • Use the OnAttributeUpdated event to trigger UI or gameplay updates.
  • Use clamping and formulas as defined in the template for robust attribute logic.
⚠️ **GitHub.com Fallback** ⚠️