ItemEquippable - jimdroberts/FishMMO GitHub Wiki

Description

Represents the equippable component of an item, handling equip/unequip logic and owner tracking. Manages events for when the item is equipped or unequipped by a character.


API Access

Fields

  • private Item item

    The item instance this equippable component belongs to.

  • public event Action OnEquip

    Event triggered when the item is equipped by a character.

  • public event Action OnUnequip

    Event triggered when the item is unequipped by a character.

Properties

  • public ICharacter Character { get; private set; }

    The character currently owning/equipping this item. Null if not equipped.

Methods

  • public void Initialize(Item item)

    Initializes the equippable component with its parent item. Parameters: - Item item: The item instance.*

  • public void Destroy()

    Destroys the equippable component, ensuring it is unequipped and cleaned up.

  • public void Equip(ICharacter owner)

    Equips the item to the specified character, firing the OnEquip event. If already equipped, unequips first. Parameters: - ICharacter owner: The character to equip the item to.*

  • public void Unequip()

    Unequips the item from the current character, firing the OnUnequip event.

  • public void SetOwner(ICharacter owner)

    Sets the owner of this equippable item directly. Used for fast assignment. Parameters: - ICharacter owner: The character to set as owner.*


Basic Usage

Setup

  1. Create an ItemEquippable instance and initialize it with its parent Item.
  2. Use Equip to assign the item to a character and Unequip to remove it.
  3. Subscribe to OnEquip and OnUnequip events to handle equip/unequip logic in your systems.

Example

// Example 1: Equipping and unequipping an item
ItemEquippable equippable = new ItemEquippable();
equippable.Initialize(item);
equippable.Equip(character); // Equip to character
equippable.Unequip(); // Unequip from character

// Example 2: Handling equip/unequip events
equippable.OnEquip += (c) => Debug.Log($"Equipped by {c}");
equippable.OnUnequip += (c) => Debug.Log($"Unequipped by {c}");

Best Practices

  • Always call Initialize before using Equip or Unequip to ensure the component is linked to its item.
  • Use events to trigger additional logic when items are equipped or unequipped.
  • Use SetOwner for direct assignment when needed, but prefer Equip/Unequip for full event handling.
  • Clean up event subscriptions to avoid memory leaks.
⚠️ **GitHub.com Fallback** ⚠️