Shopkeeper

Scripting with the Shopkeeper class.

A Shopkeeper is a merchant that buys and sells items.

Fields & Properties

Name
Description
Type
Access

items

A list of items that this shopkeeper sells.

List<Item>

public

activeItemTypeFilter

The active item type filter.

ItemType

public

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