UIPetControl - jimdroberts/FishMMO GitHub Wiki

Description

UIPetControl manages the UI elements and interactions for a player's pet in the FishMMO client. It displays the pet's name and health, subscribes to pet events, and provides UI controls for pet commands such as follow, stay, summon, and release.


API Access

Fields

  • public TMP_Text PetNameLabel

    The label displaying the pet's name.

  • public Slider PetHealth

    The slider displaying the pet's health.

Methods

  • public override void OnPostSetCharacter()

    Called after the character is set. Subscribes to pet controller events and updates pet UI.

  • public override void OnPreUnsetCharacter()

    Called before the character is unset. Unsubscribes from pet controller events and resets pet UI.

  • public void PetController_OnPetSummoned(Pet pet)

    Handles pet summoned event. Updates pet UI and makes it visible. Parameters: Pet pet – The summoned pet.

  • public void PetController_OnPetDestroyed()

    Handles pet destroyed event. Hides the pet UI.

  • private bool HasPet()

    Returns true if the character has a pet. Returns: bool – True if pet exists, false otherwise.

  • public void OnFollowPet()

    Sends a follow command to the pet.

  • public void OnStayPet()

    Sends a stay command to the pet.

  • public void OnSummonPet()

    Sends a summon command to the pet.

  • public void OnReleasePet()

    Sends a release command to the pet.


Basic Usage

Setup

  1. Ensure the UI GameObject has a UIPetControl component attached.
  2. Assign the PetNameLabel and PetHealth fields in the Inspector.
  3. The character must implement IPetController and be set on this control.
  4. The component subscribes to pet events automatically on character set.

Example

// Example: Setting up UIPetControl in a scene
// 1. Attach UIPetControl to a UI GameObject.
// 2. Assign the PetNameLabel and PetHealth references in the Inspector.
// 3. Set the character implementing IPetController.

UIPetControl petControl = GetComponent<UIPetControl>();
petControl.PetNameLabel = ...; // Assign TMP_Text
petControl.PetHealth = ...;    // Assign Slider
petControl.SetCharacter(playerCharacter); // Set character

Best Practices

  • Always assign PetNameLabel and PetHealth in the Inspector to avoid null references.
  • Ensure the character implements IPetController for full functionality.
  • Unsubscribe from events in OnPreUnsetCharacter to prevent memory leaks.
  • Use the provided OnFollowPet, OnStayPet, OnSummonPet, and OnReleasePet methods for UI button actions.
⚠️ **GitHub.com Fallback** ⚠️