4. Your First Custom Command¶
A command is just a Python function (like Unit 3, Lesson 6) wrapped the way Evennia expects. Open mudgame/commands/command.py and add a new class:
from evennia.commands.command import Command as BaseCommand
class CmdWave(BaseCommand):
"""
wave at everyone in the room
Usage:
wave
"""
key = "wave"
locks = "cmd:all()"
def func(self):
self.caller.location.msg_contents(f"{self.caller.name} waves.")
key is what the player types. func() is what runs — self.caller is the character using the command, and .location.msg_contents(...) sends a message to everyone in the room.
A new command doesn't do anything until it's added to a command set. Open mudgame/commands/default_cmdsets.py:
from evennia import default_cmds
from commands.command import CmdWave
class CharacterCmdSet(default_cmds.CharacterCmdSet):
key = "DefaultCharacter"
def at_cmdset_creation(self):
super().at_cmdset_creation()
self.add(CmdWave())
Save both files, then @reload from your MUD tile, and try it:
> wave
You wave.
Try it
Add CmdWave exactly as above, @reload, and run wave. Then change the message text to something of your own and reload again.
Tip
Every new command needs two edits: the command class in command.py, and the self.add(...) line in default_cmdsets.py that actually turns it on.