Shopkeeper

🛍️ Shopkeeper Class

A merchant that buys and sells items.

Namespace: Esper.Inventool.Merchants Access: public Type: class Inherits: Merchant


📦 Fields

Access
Field
Type
Description

public

items

List<Item>

A list of items that this shopkeeper sells.

public

activeItemTypeFilter

ItemType

The active item type filter.

public static

onItemSold

UnityEvent<ItemStack>

Callback for when an item is sold. Accepts the sold ItemStack.

public static

onItemBought

UnityEvent<ItemStack>

Callback for when an item is bought. Accepts the bought ItemStack.


🧰 Methods

Access
Method
Returns
Description

public

GetFilterTypes()

List<ItemType>

Gets a list of item filters based on the shop’s inventory.

public

GetItems()

List<Item>

Gets the list of items, taking into account the active item filter.

public

SetItemFilter(ItemType itemType)

void

Sets the active item type filter.

public

RemoveItemFilter()

void

Removes the active item type filter.

public

CanBuyItem(Item item, int quantity, bool invokeFailureReason = false)

bool

Checks if a specific item can be bought.

public

TryBuyItem(Item item, int quantity)

bool

Attempts to buy an item. Returns true if successful.

public

CanSellItem(ItemStack itemStack, bool invokeFailureReason = false)

bool

Checks if a specific item stack can be sold.

public

TrySellItem(ItemStack itemStack, int quantity)

bool

Attempts to sell an item stack. Returns true if successful.

public

Clean()

void

Cleans the object by removing deleted items.

public

Save() (Editor only)

void

Saves the object.


🧩 Nested Types

public enum TransactionStepType

Defines how transactions are handled.

Name
Description

Instant

Instantly buy or sell an item.

Confirmation

Require confirmation when buying or selling an item.

QuantitySelection

Require confirmation and allow the user to select the quantity.


Creating Shopkeepers

It's not recommended to create shopkeepers at runtime, but it is possible. Shopkeeper's are scriptable objects which cannot be created as assets at runtime for later use.

Shopekeeper myShopkeeper = ScriptableObject.CreateInstance<Shopkeeper>();

Filters

Get Filters

You can get all ItemType filters of a shopkeeper with the GetFilterTypes methods.

var myfilters = myShopkeeper.GetFilterTypes();

Set Filter

By setting the filter, only the items that match the filter will be returned by the GetItems method. You will need a reference to an Item Type to set the filter.

myShopekeeper.SetItemFilter(myItemType);

Remove Filter

myShopkeeper.RemoveItemFilter();

Get Items

You can get items wit the GetItems method. If no filter is active, all items will be returned. Otherwise, only the items that match the filter will be returned.

var items = myShopkeeper.GetItems();

Buying Items

You can but an item from a shopkeeper with the TryBuyItem method. This method returns true if the item was bought successfully. You will need a reference to an Item. Buying may fail if the player does not have sufficient funds, lacking space in their inventory, or the shopkeeper does not sell the item. If successful, the item will appear in the player's inventory, and the cost will be subtracted from their funds.

quantity is an int that sets the amount of the item to purchase.

bool result = myShopkeeper.TryBuyItem(myItem, quantity);

Before committing to this method, you have the option of checking if the item can be bought with CanBuyItem method.

bool result = myShopkeeper.CanBuyItem(myItem, quantity);

Selling Items

You can sell an ItemStack to a shopkeeper with the TrySellItem method. This method returns true if the item was sold successfully. You will need a reference to an ItemStack. Selling may fail if the player does not own the item or the item is not sellable. If successful, the item will be removed from the player's inventory and the selling cost will be added to the player's funds.

quantity is an int that sets the amount of the item to sell.

bool result = myShopkeeper.TrySellItem(myItemStack, quantity);

Before committing to this method, you have the option of checking if the item can be sold with CanSellItem method.

bool result = myShopkeeper.CanSellItem(myItemStack);

Last updated