-
Notifications
You must be signed in to change notification settings - Fork 8
Bartering
There is a built-in rudimentary barter system- the BarterSystem
. It is a non-autoload node that is used to facilitate the transfer of items and currency from one inventory to another.
The barter process works by creating a Transaction
, which stores items to be bought and sold. The actual barter process is adding items to this Transaction. once the player is satisfied, the Transaction is accepted, and resolves, which moves items to the proper inventories and moves currency around accordingly. It is done in this way to allow easy cancellation of a transaction and to hopefully mitigate item duping exploits, since nothing is actually moved into any inventory until accepted.
It may help to conceptualize the decisions made with the functions to imagine this being the backend for Morrowind's barter screen, which is what I imagined this looking like in my head while I made it.
A barter transaction needs two InventoryComponents
to begin - the Customer
and the Vendor
. Practically, it doesn't matter who is who, but conventionally this would be the player and an in-game merchant, respectively.
There are 4 functions the BarterSystem has to work with:
-
start_barter
begins the barter process with two providedInventoryComponent
s. -
sell_item
will sell an item from the customer to the vendor. Will return false if the item cannot be sold, or is cancelling a buy. -
buy_item
will sell an item from the vendor to the customer. Will return false if the item cannot be bought, or is cancelling a sell. -
cancel_barter
will cancel the barter process. -
accept_barter
will resolve the transaction, and move items and currency around. It requires two numbers passed in, which correspond to multipliers given to the money moved from the customer to the vendor (buying) or vice versa. This is to allow, say for a necklace that gives 25% better prices, or a speechcraft skill, or something. In the future, there may also be per-item haggling support. Note that all prices are rounded to whole numbers after the modifier. Will return false if either part doesn't have enough money to complete the transaction.
All functions except cancel_barter
return a boolean to indicate whether they succeeded or failed.
And a few signals:
-
begun_barter
is emitted when the barter process begins. -
ended_barter
is emitted when the barter process is ended - cancelled or accepted. -
cancelled_barter
is emitted when the barter is cancelled. Note thatended_barter
is also called when this happens. To cancel buying an item, pass it tosell_item
, and vice versa, and it will be removed from the transaction.