Adding Command Tutorial¶
This is a quick first-time tutorial expanding on the Commands documentation.
mygamehere). 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. This is just one place commands could be placed but you get it setup from the onset as an easy place to start. It also 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 docstring. A docstring is the string at the very top of a class or function/method. The docstring at the top of the command class is read by Evennia to become the help entry for the Command (see Command Auto-help).
- Define a class method
func(self)that echoes your input back to you.
Below is an example how this all could look for the echo command:
# 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¶
- 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())
@reloadcommand. You should now be able to use your new
echocommand from inside
help echoto see the documentation for the command.
echotestwill also work. It will be handled as the command
echodirectly followed byits argument
test(which will end up in
self.args). To change this behavior, you can add thearg_regex
,help_category` etc. See the arg_regex documentation for more info.
get), just add your new
@reloadto see any changes.
Adding the command to specific object types¶
CharacterCmdSetis just one easy exapmple. The cmdset system is very
mycmdsets.py) and add them to
# 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
@reloadthe merger will
permanent=Truekeyword to the
cmdset.addcall. This will however
cmdset.addcall in your
# 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
at_object_creationis only called once, when the
@py from typeclasses.objects import MyObject; [o.cmdset.add("mycmdset.MyCmdSet") for o in MyObject.objects.all()]
@reloadto make the Command immediately available.
Change where Evennia looks for command sets¶
evennia/settings_default.py for the other settings.
documentation: Command-Sets .. _Typeclasses: Typeclasses.html