Commands are intimately linked to Command Sets and you need to read that page too to be familiar with how the command system works. The two pages were split for easy reading.
The basic way for users to communicate with the game is through Commands. These can be commands directly related to the game world such as look, get, drop and so on, or administrative commands such as examine or @dig.
The default commands coming with Evennia are ‘MUX-like’ in that they
use @ for admin commands, support things like switches, syntax with the
‘=’ symbol etc, but there is nothing that prevents you from implementing
a completely different command scheme for your game. You can find the
default commands in
evennia/commands/default. You should not edit
these directly - they will be updated by the Evennia team as new
features are added. Rather you should look to them for inspiration and
inherit your own designs from them.
There are two components to having a command running - the Command class and the Command Set (command sets were split into a separate wiki page for ease of reading).
- A Command is a python class containing all the functioning code for what a command does - for example, a get command would contain code for picking up objects.
- A Command Set (often referred to as a CmdSet or cmdset) is like a container for one or more Commands. A given Command can go into any number of different command sets. Only by putting the command set on a character object you will make all the commands therein available to use by that character. You can also store command sets on normal objects if you want users to be able to use the object in various ways. Consider a “Tree” object with a cmdset defining the commands climb and chop down. Or a “Clock” with a cmdset containing the single command check time.
This page goes into full detail about how to use Commands. To fully use them you must also read the page detailing Command Sets. There is also a step-by-step Adding Command Tutorial that will get you started quickly without the extra explanations.
All commands are implemented as normal Python classes inheriting from
the base class
evennia.Command). You will find that
this base class is very “bare”. The default commands of Evennia actually
inherit from a child of
MuxCommand - this is the
class that knows all the mux-like syntax like
by “=” etc. Below we’ll avoid mux-specifics and use the base
# basic Command definition from evennia import Command class MyCmd(Command): """ This is the help-text for the command """ key = "mycommand" def parse(self): # parsing the command line here def func(self): # executing the command here
You define a new command by assigning a few class-global properties on your inherited class and overloading one or two hook functions. The full gritty mechanic behind how commands work are found towards the end of this page; for now you only need to know that the command handler creates an instance of this class and uses that instance whenever you use this command - it also dynamically assigns the new command instance a few useful properties that you can assume to always be available.
Who is calling the command?¶
In Evennia there are three types of objects that may call the command.
It is important to be aware of this since this will also assign
properties on the command body at runtime. Most often the calling type
- A Session. This is by far the most common case when a user is
entering a command in their client.
caller- this is set to the puppeted Object if such an object exists. If no puppet is found,
calleris set equal to
player. Only if a Player is not found either (such as before being logged in) will this be set to the Session object itself.
session- a reference to the Session object itself.
sessid.id, a unique integer identifier of the session.
player- the Player object connected to this Session. None if not logged in.
- A Player. This only happens if
player.execute_cmd()was used. No Session information can be obtained in this case.
caller- this is set to the puppeted Object if such an object can be determined (without Session info this can only be determined in
1). If no puppet is found, this is equal to
player- Set to the Player object.
- An Object. This only happens if
object.execute_cmd()was used (for example by an NPC).
caller- This is set to the calling Object in question.
Properties assigned to the command instance at run-time¶
Let’s say player Bob with a character BigGuy enters the command
look at sword. After the system having successfully identified this as
the “look” command and determined that BigGuy really has access to a
look, it chugs the
look command class out of
storage and either loads an existing Command instance from cache or
creates one. After some more checks it then assigns it the following
caller- The character BigGuy, in this example. This is a reference to the object executing the command. The value of this depends on what type of object is calling the command; see the previous section.
session- the Session Bob uses to connect to the game and control BigGuy (see also previous section).
sessid- the unique id of
self.session, for quick lookup.
player- the Player Bob (see previous section).
cmdstring- the matched key for the command. This would be look in our example.
args- this is the rest of the string, except the command name. So if the string entered was look at sword,
argswould be ” at sword“. Note the space kept - Evennia would correctly interpret
lookat swordtoo. This is useful for things like
/switchesthat should not use space. In the
MuxCommandclass used for default commands, this space is stripped. Also see the
arg_regexproperty if you want to enforce a space to make
lookat swordgive a command-not-found error.
obj- the game Object on which this command is defined. This need not be the caller, but since
lookis a common (default) command, this is probably defined directly on BigGuy - so
objwill point to BigGuy. Otherwise
objcould be a Player or any interactive object with commands defined on it, like in the example of the “check time” command defined on a “Clock” object.
cmdset- this is a reference to the merged CmdSet (see below) from which this command was matched. This variable is rarely used, it’s main use is for the auto-help system (Advanced note: the merged cmdset need NOT be the same as ``BigGuy.cmdset``. The merged set can be a combination of the cmdsets from other objects in the room, for example).
raw_string- this is the raw input coming from the user, without stripping any surrounding whitespace. The only thing that is stripped is the ending newline marker.
Defining your own command classes¶
Beyond the properties Evennia always assigns to the command at run-time (listed above), your job is to define the following class properties:
key(string) - the identifier for the command, like
look. This should (ideally) be unique. A key can consist of more than one word, like “press button” or “pull left lever”. Note that both
aliasesbelow determine the identity of a command. So two commands are considered if either matches. This is important for merging cmdsets described below.
aliases(optional list) - a list of alternate names for the command (
["glance", "see", "l"]). Same name rules as for
locks(string) - a lock definition, usually on the form
cmd:<lockfuncs>. Locks is a rather big topic, so until you learn more about locks, stick to giving the lockstring
"cmd:all()"to make the command available to everyone (if you don’t provide a lock string, this will be assigned for you).
help_category(optional string) - setting this helps to structure the auto-help into categories. If none is set, this will be set to General.
save_for_next(optional boolean). This defaults to
True, a copy of this command object (along with any changes you have done to it) will be stored by the system and can be accessed by the next command by retrieving
self.caller.ndb.last_cmd. The next run command will either clear or replace the storage.
arg_regex(optional raw string): This should be given as a raw regular expression string. The regex will be compiled by the system at runtime. This allows you to customize how the part immediately following the command name (or alias) must look in order for the parser to match for this command. Normally the parser is highly efficient in picking out the command name, also as the beginning of a longer word (as long as the longer word is not a command name in it self). So
"lookme"will be parsed as the command
"look"followed by the argument
"me". By using
arg_regexyou could for example force the parser to require the command name to be followed by a space and then some other argument (regex
r"\s.+"). Or you could allow both that and a stand-alone command (regex
r"\s.+|$"). In this case,
"look me"will work whereas
"lookme"will lead to a “command not found” error.
auto_help(optional boolean). Defaults to
True. This allows for turning off the auto-help system on a per-command basis. This could be useful if you either want to write your help entries manually or hide the existence of a command from
help’s generated list.
is_exit(bool) - this marks the command as being used for an in-game exit. This is, by default, set by all Exit objects and you should not need to set it manually unless you make your own Exit system. It is used for optimization and allows the cmdhandler to easily disregard this command when the cmdset has its
is_channel(bool)- this marks the command as being used for an in-game channel. This is, by default, set by all Channel objects and you should not need to set it manually unless you make your own Channel system. is used for optimization and allows the cmdhandler to easily disregard this command when its cmdset has its
msg_all_sessions(bool): This affects the behavior of the
Command.msgmethod. If unset (default), calling
self.msg(text)from the Command will always only send text to the Session that actually triggered this Command. If set however,
self.msg(text)will send to all Sessions relevant to the object this Command sits on. Just which Sessions receives the text depends on the object and the server’s
You should also implement at least two methods,
func() (You could also implement
perm(), but that’s not needed
unless you want to fundamentally change how access checks work).
at_pre_cmd()is called very first on the command. If this function returns anything that evaluates to
Truethe command execution is aborted at this point.
parse()is intended to parse the arguments (
self.args) of the function. You can do this in any way you like, then store the result(s) in variable(s) on the command object itself (i.e. on
self). To take an example, the default mux-like system uses this method to detect “command switches” and store them as a list in
self.switches. Since the parsing is usually quite similar inside a command scheme you should make
parse()as generic as possible and then inherit from it rather than re-implementing it over and over. In this way, the default
MuxCommandclass implements a
parse()for all child commands to use.
func()is called right after
parse()and should make use of the pre-parsed input to actually do whatever the command is supposed to do. This is the main body of the command. The return value from this method will be returned from the execution as a Twisted Deferred.
at_post_cmd()is called after
func()to handle eventual cleanup.
Finally, you should always make an informative doc string
__doc__) at the top of your class. This string is dynamically read
by the Help System to create the help entry for this command. You
should decide on a way to format your help and stick to that.
Below is how you define a simple alternative “
from evennia import Command class CmdSmile(Command): """ A smile command Usage: smile [at] [<someone>] grin [at] [<someone>] Smiles to someone in your vicinity or to the room in general. (This initial string (the __doc__ string) is also used to auto-generate the help for this command) """ key = "smile" aliases = ["smile at", "grin", "grin at"] locks = "cmd:all()" help_category = "General" def parse(self): "Very trivial parser" self.target = self.args.strip() def func(self): "This actually does things" caller = self.caller if not self.target or self.target == "here": string = "%s smiles." % caller.name caller.location.msg_contents(string, exclude=caller) caller.msg("You smile.") else: target = caller.search(self.target) if not target: # caller.search handles error messages return string = "%s smiles to you." % caller.name target.msg(string) string = "You smile to %s." % target.name caller.msg(string) string = "%s smiles to %s." % (caller.name, target.name) caller.location.msg_contents(string, exclude=[caller,target])
The power of having commands as classes and to separate
func() lies in the ability to inherit functionality without having
to parse every command individually. For example, as mentioned the
default commands all inherit from
implements its own version of
parse() that understands all the
specifics of MUX-like commands. Almost none of the default commands thus
need to implement
parse() at all, but can assume the incoming string
is already split up and parsed in suitable ways by its parent.
Before you can actually use the command in your game, you must now store it within a command set. See the Command Sets page.
Note: This is an advanced topic. Skip it if this is your first time learning about commands.
There are several command-situations that are exceptional in the eyes of the server. What happens if the player enters an empty string? What if the ‘command’ given is infact the name of a channel the user wants to send a message to? Or if there are multiple command possibilities?
Such ‘special cases’ are handled by what’s called system commands. A
system command is defined in the same way as other commands, except that
their name (key) must be set to one reserved by the engine (the names
are defined at the top of
evennia/commands/cmdhandler.py). You can
find (unused) implementations of the system commands in
evennia/commands/default/system_commands.py. Since these are not (by
default) included in any
CmdSet they are not actually used, they are
just there for show. When the special situation occurs, Evennia will
look through all valid
CmdSets for your custom system command.
Only after that will it resort to its own, hard-coded implementation.
Here are the exceptional situations that triggers system commands. You
can find the command keys they use as properties on
- No input (
syscmdkeys.CMD_NOINPUT) - the player just pressed return without any input. Default is to do nothing, but it can be useful to do something here for certain implementations such as line editors that interpret non-commands as text input (an empty line in the editing buffer).
- Command not found (
syscmdkeys.CMD_NOMATCH) - No matching command was found. Default is to display the “Huh?” error message.
- Several matching commands where found (
syscmdkeys.CMD_MULTIMATCH) - Default is to show a list of matches.
- User is not allowed to execute the command
syscmdkeys.CMD_NOPERM) - Default is to display the “Huh?” error message.
- Channel (
syscmdkeys.CMD_CHANNEL) - This is a Channel name of a channel you are subscribing to - Default is to relay the command’s argument to that channel. Such commands are created by the Comm system on the fly depending on your subscriptions.
- New session connection (
syscmdkeys.CMD_LOGINSTART). This command name should be put in the
settings.CMDSET_UNLOGGEDIN. Whenever a new connection is established, this command is always called on the server (default is to show the login screen).
Below is an example of redefining what happens when the player doesn’t provide any input (e.g. just presses return). Of course the new system command must be added to a cmdset as well before it will work.
from evennia import syscmdkeys, Command class MyNoInputCommand(Command): "Usage: Just press return, I dare you" key = syscmdkeys.CMD_NOINPUT def func(self): self.caller.msg("Don't just press return like that, talk to me!")
Note: This is an advanced topic.
Normally Commands are created as fixed classes and used without modification. There are however situations when the exact key, alias or other properties is not possible (or impractical) to pre-code (Exits is an example of this).
To create a command with a dynamic call signature, first define the
command body normally in a class (set your
default values), then use the following call (assuming the command class
you created is named
cmd = MyCommand(key="newname", aliases=["test", "test2"], locks="cmd:all()", ...)
All keyword arguments you give to the Command constructor will be stored as a property on the command object. This will overload existing properties defined on the parent class.
Normally you would define your class and only overload things like
aliases at run-time. But you could in principle also
send method objects (like
func) as keyword arguments in order to
make your command completely customized at run-time.
Note: This is an advanced topic.
Exits are examples of the use of a Dynamic Command.
traverse_*hooks on the Exit object. But if you are interested in really changing how things work under the hood, check out
evennia/objects/objects.pyfor how the
Exittypeclass is set up.
How commands actually work¶
Note: This is an advanced topic mainly of interest to server developers.
Any time the user sends text to Evennia, the server tries to figure out if the text entered corresponds to a known command. This is how the command handler sequence looks for a logged-in user:
- A user enters a string of text and presses enter.
- The user’s Session determines the text is not some protocol-specific control sequence or OOB command, but sends it on to the command handler.
- Evennia’s command handler analyzes the Session and grabs eventual references to Player and eventual puppeted Characters (these will be stored on the command object later). The caller property is set appropriately.
- If input is an empty string, resend command as
CMD_NOINPUT. If no such command is found in cmdset, ignore.
- If command.key matches
settings.IDLE_COMMAND, update timers but don’t do anything more.
- The command handler gathers the CmdSets available to caller at this time:
- The caller’s own currently active CmdSet.
- CmdSets defined on the current player, if caller is a puppeted object.
- CmdSets defined on the Session itself.
- The active CmdSets of eventual objects in the same location (if any). This includes commands on Exits.
- Sets of dynamically created System commands representing available Communications.
- All !CmdSets of the same priority are merged together in groups. Grouping avoids order-dependent issues of merging multiple same-prio sets onto lower ones.
- All the grouped CmdSets are merged in reverse priority into one combined CmdSet according to each set’s merge rules.
- Evennia’s command parser takes the merged cmdset and matches each of its commands (using its key and aliases) against the beginning of the string entered by caller. This produces a set of candidates.
- The cmd parser next rates the matches by how many characters they have and how many percent matches the respective known command. Only if candidates cannot be separated will it return multiple matches.
- If multiple matches were returned, resend as
CMD_MULTIMATCH. If no such command is found in cmdset, return hard-coded list of matches.
- If no match was found, resend as
CMD_NOMATCH. If no such command is found in cmdset, give hard-coded error message.
- If a single command was found by the parser, the correct command object is plucked out of storage. This usually doesn’t mean a re-initialization.
- It is checked that the caller actually has access to the command by
validating the lockstring of the command. If not, it is not
considered as a suitable match and
- If the new command is tagged as a channel-command, resend as
CMD_CHANNEL. If no such command is found in cmdset, use hard-coded implementation.
- Assign several useful variables to the command instance (see previous sections).
at_pre_command()on the command instance.
parse()on the command instance. This is fed the remainder of the string, after the name of the command. It’s intended to pre-parse the string into a form useful for the
func()on the command instance. This is the functional body of the command, actually doing useful things.
at_post_command()on the command instance.
The return value of
Command.func() is a Twisted deferred. Evennia
does not use this return value at all by default. If you do, you must
thus do so asynchronously, using callbacks.
# in command class func() def callback(ret, caller): caller.msg("Returned is %s" % ret) deferred = self.execute_command("longrunning") deferred.addCallback(callback, self.caller)
This is probably not relevant to any but the most advanced/exotic designs (one might use it to create a “nested” command structure for example).
save_for_next class variable can be used to implement
state-persistent commands. For example it can make a command operate on
“it”, where it is determined by what the previous command operated on.