Merchant - jimdroberts/FishMMO GitHub Wiki

Description

Represents a merchant NPC that players can interact with to buy or sell items. Inherits from Interactable and uses a MerchantTemplate for configuration.


API Access

Fields

  • public MerchantTemplate Template

    The template containing merchant configuration, such as inventory and description.

  • private string title

    The display title for this merchant, shown in the UI. Defaults to "Merchant" but can be set from the template.

Properties

  • public override string Title { get; }

    Gets the display title for this merchant, used in UI elements.

Methods

  • public override void OnAwake()

    Called when the merchant is awakened in the scene. Sets the title from the template description if available.

  • public override bool CanInteract(IPlayerCharacter character)

    Determines if the specified player character can interact with this merchant. Requires a valid template and that the base interaction checks pass. character (IPlayerCharacter): The player character attempting to interact. Returns: True if interaction is allowed, false otherwise.


Basic Usage

Setup

  1. Attach the Merchant component to an NPC GameObject in your scene.
  2. Assign a MerchantTemplate asset to the Template field in the Inspector.
  3. The merchant will display the title from the template's description in the UI.
  4. Players can interact with the merchant to buy or sell items if the template is assigned.

Example

// Example 1: Setting up a Merchant NPC in the Unity Editor
// 1. Add the Merchant script to an NPC GameObject.
// 2. Assign a MerchantTemplate asset to the Template field.
// 3. The merchant will now use the template's description as its title.

// Example 2: Accessing the title in code
var merchant = GetComponent<Merchant>();
Debug.Log(merchant.Title);

Best Practices

  • Always assign a MerchantTemplate to ensure the merchant is functional and displays the correct title.
  • Use the template to configure inventory, abilities, and other merchant properties.
  • Override OnAwake for additional initialization logic if needed.
  • Use CanInteract to enforce custom interaction rules for merchants.
⚠️ **GitHub.com Fallback** ⚠️