NPC merchants¶
*** Welcome to ye Old Sword shop! ***
Things for sale (choose 1-3 to inspect, quit to exit):
_________________________________________________________
1. A rusty sword (5 gold)
2. A sword with a leather handle (10 gold)
3. Excalibur (100 gold)
This will introduce an NPC able to sell things. In practice this means that when you interact with them you’ll get shown a menu of choices. Evennia provides the EvMenu utility to easily create in-game menus.
We will store all the merchant’s wares in their inventory. This means that they may stand in an actual shop room, at a market or wander the road. We will also use ‘gold’ as an example currency.
To enter the shop, you’ll just need to stand in the same room and use the buy/shop
command.
Making the merchant class¶
The merchant will respond to you giving the shop
or buy
command in their presence.
# in for example mygame/typeclasses/merchants.py
from typeclasses.objects import Object
from evennia import Command, CmdSet, EvMenu
class CmdOpenShop(Command):
"""
Open the shop!
Usage:
shop/buy
"""
key = "shop"
aliases = ["buy"]
def func(self):
# this will sit on the Merchant, which is self.obj.
# the self.caller is the player wanting to buy stuff.
self.obj.open_shop(self.caller)
class MerchantCmdSet(CmdSet):
def at_cmdset_creation(self):
self.add(CmdOpenShop())
class NPCMerchant(Object):
def at_object_creation(self):
self.cmdset.add_default(MerchantCmdSet)
def open_shop(self, shopper):
menunodes = {} # TODO!
shopname = self.db.shopname or "The shop"
EvMenu(shopper, menunodes, startnode="shopfront",
shopname=shopname, shopkeeper=self, wares=self.contents)
We could also have put the commands in a separate module, but for compactness, we put it all with the merchant typeclass.
Note that we make the merchant an Object
! Since we don’t give them any other commands, it makes little sense to let them be a Character
.
We make a very simple shop
/buy
Command and make sure to add it on the merchant in its own cmdset.
We initialize EvMenu
on the shopper
but we haven’t created any menunodes
yet, so this will not actually do much at this point. It’s important that we we pass shopname
, shopkeeper
and wares
into the menu, it means they will be made available as properties on the EvMenu instance - we will be able to access them from inside the menu.
The shop is open for business!¶
Make sure to reload
.
Let’s try it out by creating the merchant and a few wares in-game. Remember that we also must create some gold get this economy going.
> set self/gold = 8
> create/drop Stan S. Stanman;stan:typeclasses.merchants.NPCMerchant
> set stan/shopname = Stan's previously owned vessles
> create/drop A proud vessel;ship
> set ship/desc = The thing has holes in it.
> set ship/gold_value = 5
> create/drop A classic speedster;rowboat
> set rowboat/gold_value = 2
> set rowboat/desc = It's not going anywhere fast.
Note that a builder without any access to Python code can now set up a personalized merchant with just in-game commands. With the shop all set up, we just need to be in the same room to start consuming!
> buy
*** Welcome to Stan's previously owned vessels! ***
Things for sale (choose 1-3 to inspect, quit to exit):
_________________________________________________________
1. A proud vessel (5 gold)
2. A classic speedster (2 gold)
> 1
You inspect A proud vessel:
The thing has holes in it.
__________________________________________________________
1. Buy A proud vessel (5 gold)
2. Look for something else.
> 1
You pay 5 gold and purchase A proud vessel!
*** Welcome to Stan's previously owned vessels! ***
Things for sale (choose 1-3 to inspect, quit to exit):
_________________________________________________________
1. A classic speedster (2 gold)