Adding Command Tutorial¶
This is a quick first-time tutorial expanding on the Commands documentation.
Let’s assume you have just downloaded Evennia, installed it and created
your game folder (let’s call it just
mygame here). Now you want to
try to add a new command. This is the fastest way to do it.
Step 1: Creating a custom command¶
mygame/commands/command.pyin a text editor. It already contains some example code.
- Create a new class in
default_cmds.MuxCommand. Let’s call it
CmdEchoin this example.
- Set the class variable
keyto a good command name, like
- Give your class a useful
__doc__string, this will become the help entry for the Command (see Command Auto-help).
- Define a class method
func()that does stuff.
Below is an example how this all could look:
# file mygame/commands/command.py #[...] from evennia import default_cmds class CmdEcho(default_cmds.MuxCommand): """ Simple command example Usage: echo [text] This command simply echoes text back to the caller. """ key = "echo" def func(self): "This actually does things" if not self.args: self.caller.msg("You didn't enter anything!") else: self.caller.msg("You gave the string: '%s'" % self.args)
Step 2: Adding the Command to a default Cmdset¶
The command is not available to use until it is part of a Command Set. In this example we will go the easiest route and add it to the default Character commandset that already exists.
- Import your new command with
from commands.command import CmdEcho.
- Add a line
CharacterCmdSet, in the
at_cmdset_creationmethod (the template tells you where).
This is approximately how it should look at this point:
# file mygame/commands/default_cmdsets.py #[...] from commands.command import CmdEcho #[...] class CharacterCmdSet(default_cmds.CharacterCmdSet): key = "DefaultCharacter" def at_cmdset_creation(self): # this first adds all default commands super(DefaultSet, self).at_cmdset_creation() # all commands added after this point will extend or # overwrite the default commands. self.add(CmdEcho())
Next, run the
@reload command. You should now be able to use your
echo command from inside the game. Use
help echo to see the
documentation for the command.
If you have trouble, make sure to check the log for error messages (probably due to syntax errors in your command definition).
If you want to overload existing default commands (such as
get), just add your new command with the same key as the old one -
it will then replace it. Just remember that you must use
see any changes.
See Commands for many more details and possibilities when defining Commands and using Cmdsets in various ways.
Adding the command to specific object types¶
Adding your Command to the
CharacterCmdSet is just one easy
exapmple. The cmdset system is very generic. You can create your own
cmdsets (let’s say in a module
mycmdsets.py) and add them to objects
as you please (how to control their merging is described in detail in
the Command Set documentation).
# file mygame/commands/mycmdsets.py #[...] from commands.command import CmdEcho from evennia import CmdSet #[...] class MyCmdSet(CmdSet): key = "MyCmdSet" def at_cmdset_creation(self): self.add(CmdEcho())
Now you just need to add this to an object. To test things (as superuser) you can do
This will add this cmdset (along with its echo command) to yourself so you can test it. Note that you cannot add a single Command to an object on its own, it must be part of a CommandSet in order to do so.
The Command you added is not there permanently at this point. If you do
@reload the merger will be gone. You could add the
permanent=True keyword to the
cmdset.add call. This will however
only make the new merged cmdset permanent on that single object. Often
you want all objects of this particular class to have this cmdset.
To make sure all new created objects get your new merged set, put the
cmdset.add call in your custom Typeclasses’
# e.g. in mygame/typeclasses/objects.py from evennia import DefaultObject class MyObject(DefaultObject): def at_object_creation(self): "called when the object is first created" self.cmdset.add("mycmdset.MyCmdSet", permanent=True)
All new objects of this typeclass will now start with this cmdset and it
will survive a
Note: An important caveat with this is that
only called once, when the object is first created. This means that if
you already have existing objects in your databases using that
typeclass, they will not have been initiated the same way. There are
many ways to update them; since it’s a one-time update you can usually
just simply loop through them. As superuser, try the following:
@py from typeclasses.objects import MyObject; [o.cmdset.add("mycmdset.MyCmdSet") for o in MyObject.objects.all()]
This goes through all objects in your database having the right
typeclass, adding the new cmdset to each. The good news is that you only
have to do this if you want to post-add cmdsets. If you just want to
add a new command, you can simply add that command to the cmdset’s
@reload to make the Command immediately
Change where Evennia looks for command sets¶
Evennia uses settings variables to know where to look for its default command sets. These are normally not changed unless you want to re-organize your game folder in some way. For example, the default character cmdset defaults to being defined as
evennia/settings_default for the other settings.