ItemGenerator - jimdroberts/FishMMO GitHub Wiki

Description

Handles random attribute generation and management for items, including applying/removing attributes to characters. Supports equippable and template-based attribute logic, and exposes events for attribute changes.


API Access

Fields

  • protected int seed

    The random seed used for attribute generation.

  • private Dictionary<string, ItemAttribute> attributes

    Dictionary of generated item attributes, keyed by attribute name.

  • private Item item

    The item instance this generator is attached to.

  • public event Action<ItemAttribute, int, int> OnSetAttribute

    Event triggered when an attribute value is set, providing the attribute, old value, and new value.

Properties

  • public int Seed { get; set; }

    Gets or sets the seed for generation. Changing the seed triggers attribute regeneration.

  • public Dictionary<string, ItemAttribute> Attributes { get; }

    Exposes the generated attributes for external access.

Methods

  • public void Initialize(Item item, int seed)

    Initializes the generator with its parent item and seed, triggering attribute generation. Parameters: - Item item: The item instance. - int seed: The random seed for generation.*

  • public void Destroy()

    Cleans up the generator, detaching from the item.

  • public void Tooltip(ref Utf16ValueStringBuilder sb)

    Appends generator information and all generated attributes to the provided tooltip string builder. Parameters: - Utf16ValueStringBuilder sb: The string builder to append to.*

  • public void Generate()

    Triggers attribute generation using the current seed and item template.

  • public void Generate(int seed, BaseItemTemplate template = null)

    Generates item attributes using the provided seed and template. Handles equippable logic, random attributes, and additional template attributes. Parameters: - int seed: The random seed for generation. - BaseItemTemplate template: The item template to use. If null, uses the item's template.*

  • public ItemAttribute GetAttribute(string name)

    Gets the generated attribute by name, or null if not found. Parameters: - string name: The attribute name.* Returns: - ItemAttribute: The ItemAttribute instance, or null.*

  • public void SetAttribute(string name, int newValue)

    Sets the value of a generated attribute by name, firing the OnSetAttribute event if changed. Parameters: - string name: The attribute name. - int newValue: The new value to set.*

  • public void ApplyAttributes(ICharacter character)

    Applies all generated attributes to the specified character, adding values to their stats/resources. Parameters: - ICharacter character: The character to apply attributes to.*

  • public void RemoveAttributes(ICharacter character)

    Removes all generated attributes from the specified character, subtracting values from their stats/resources. Parameters: - ICharacter character: The character to remove attributes from.*


Basic Usage

Setup

  1. Create an ItemGenerator instance and initialize it with an Item and a seed value.
  2. Use Generate to create random attributes based on the item's template and seed.
  3. Use ApplyAttributes and RemoveAttributes to modify character stats/resources as items are equipped or unequipped.
  4. Subscribe to OnSetAttribute to handle attribute changes in your systems.

Example

// Example 1: Initializing and generating attributes
ItemGenerator generator = new ItemGenerator();
generator.Initialize(item, 12345); // item, seed

// Example 2: Applying and removing attributes to a character
generator.ApplyAttributes(character);
generator.RemoveAttributes(character);

// Example 3: Accessing generated attributes
foreach (var pair in generator.Attributes) {
    string attrName = pair.Key;
    int value = pair.Value.value;
}

Best Practices

  • Use a consistent seed for deterministic attribute generation.
  • Always call Destroy to clean up references when the generator is no longer needed.
  • Use events to react to attribute changes for UI or gameplay logic.
  • Document the purpose and configuration of each attribute in the template asset for clarity.
⚠️ **GitHub.com Fallback** ⚠️