mirror of
https://github.com/evennia/evennia.git
synced 2026-03-19 22:36:31 +01:00
264 lines
No EOL
16 KiB
HTML
264 lines
No EOL
16 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Default Exit Errors — Evennia 1.0-dev documentation</title>
|
||
<link rel="stylesheet" href="../_static/nature.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<link rel="shortcut icon" href="../_static/favicon.ico"/>
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
</head><body>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="General Index"
|
||
accesskey="I">index</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python Module Index"
|
||
>modules</a> |</li>
|
||
<li class="nav-item nav-item-0"><a href="../index.html">Evennia 1.0-dev</a> »</li>
|
||
<li class="nav-item nav-item-this"><a href="">Default Exit Errors</a></li>
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<div class="section" id="default-exit-errors">
|
||
<h1>Default Exit Errors<a class="headerlink" href="#default-exit-errors" title="Permalink to this headline">¶</a></h1>
|
||
<p>Evennia allows for exits to have any name. The command “kitchen” is a valid exit name as well as
|
||
“jump out the window” or “north”. An exit actually consists of two parts: an <a class="reference internal" href="../Components/Objects.html"><span class="doc">Exit Object</span></a>
|
||
and an <a class="reference internal" href="../Components/Commands.html"><span class="doc">Exit Command</span></a> stored on said exit object. The command has the same key and aliases
|
||
as the object, which is why you can see the exit in the room and just write its name to traverse it.</p>
|
||
<p>If you try to enter the name of a non-existing exit, it is thus the same as trying a non-exising
|
||
command; Evennia doesn’t care about the difference:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="o">></span> <span class="n">jump</span> <span class="n">out</span> <span class="n">the</span> <span class="n">window</span>
|
||
<span class="n">Command</span> <span class="s1">'jump out the window'</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">available</span><span class="o">.</span> <span class="n">Type</span> <span class="s2">"help"</span> <span class="k">for</span> <span class="n">help</span><span class="o">.</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Many games don’t need this type of freedom however. They define only the cardinal directions as
|
||
valid exit names (Evennia’s <code class="docutils literal notranslate"><span class="pre">@tunnel</span></code> command also offers this functionality). In this case, the
|
||
error starts to look less logical:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> > west
|
||
Command 'west' is not available. Maybe you meant "@set" or "@reset"?
|
||
</pre></div>
|
||
</div>
|
||
<p>Since we for our particular game <em>know</em> that west is an exit direction, it would be better if the
|
||
error message just told us that we couldn’t go there.</p>
|
||
<div class="section" id="adding-default-error-commands">
|
||
<h2>Adding default error commands<a class="headerlink" href="#adding-default-error-commands" title="Permalink to this headline">¶</a></h2>
|
||
<p>To solve this you need to be aware of how to <a class="reference internal" href="Starting/Part1/Adding-Commands.html"><span class="doc">write and add new commands</span></a>.
|
||
What you need to do is to create new commands for all directions you want to support in your game.
|
||
In this example all we’ll do is echo an error message, but you could certainly consider more
|
||
advanced uses. You add these commands to the default command set. Here is an example of such a set
|
||
of commands:</p>
|
||
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
10
|
||
11
|
||
12
|
||
13
|
||
14
|
||
15
|
||
16
|
||
17
|
||
18
|
||
19
|
||
20
|
||
21
|
||
22
|
||
23
|
||
24
|
||
25
|
||
26
|
||
27
|
||
28</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="c1"># for example in a file mygame/commands/movecommands.py</span>
|
||
|
||
<span class="kn">from</span> <span class="nn">evennia</span> <span class="kn">import</span> <span class="n">default_cmds</span>
|
||
|
||
<span class="k">class</span> <span class="nc">CmdExitError</span><span class="p">(</span><span class="n">default_cmds</span><span class="o">.</span><span class="n">MuxCommand</span><span class="p">):</span>
|
||
<span class="s2">"Parent class for all exit-errors."</span>
|
||
<span class="n">locks</span> <span class="o">=</span> <span class="s2">"cmd:all()"</span>
|
||
<span class="n">arg_regex</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">"\s|$"</span>
|
||
<span class="n">auto_help</span> <span class="o">=</span> <span class="bp">False</span>
|
||
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="s2">"returns the error"</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">caller</span><span class="o">.</span><span class="n">msg</span><span class="p">(</span><span class="s2">"You cannot move </span><span class="si">%s</span><span class="s2">."</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">key</span><span class="p">)</span>
|
||
|
||
<span class="k">class</span> <span class="nc">CmdExitErrorNorth</span><span class="p">(</span><span class="n">CmdExitError</span><span class="p">):</span>
|
||
<span class="n">key</span> <span class="o">=</span> <span class="s2">"north"</span>
|
||
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"n"</span><span class="p">]</span>
|
||
|
||
<span class="k">class</span> <span class="nc">CmdExitErrorEast</span><span class="p">(</span><span class="n">CmdExitError</span><span class="p">):</span>
|
||
<span class="n">key</span> <span class="o">=</span> <span class="s2">"east"</span>
|
||
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"e"</span><span class="p">]</span>
|
||
|
||
<span class="k">class</span> <span class="nc">CmdExitErrorSouth</span><span class="p">(</span><span class="n">CmdExitError</span><span class="p">):</span>
|
||
<span class="n">key</span> <span class="o">=</span> <span class="s2">"south"</span>
|
||
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"s"</span><span class="p">]</span>
|
||
|
||
<span class="k">class</span> <span class="nc">CmdExitErrorWest</span><span class="p">(</span><span class="n">CmdExitError</span><span class="p">):</span>
|
||
<span class="n">key</span> <span class="o">=</span> <span class="s2">"west"</span>
|
||
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"w"</span><span class="p">]</span>
|
||
</pre></div>
|
||
</td></tr></table></div>
|
||
<p>Make sure to add the directional commands (not their parent) to the <code class="docutils literal notranslate"><span class="pre">CharacterCmdSet</span></code> class in
|
||
<code class="docutils literal notranslate"><span class="pre">mygame/commands/default_cmdsets.py</span></code>:</p>
|
||
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
10
|
||
11
|
||
12
|
||
13</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="c1"># in mygame/commands/default_cmdsets.py</span>
|
||
|
||
<span class="kn">from</span> <span class="nn">commands</span> <span class="kn">import</span> <span class="n">movecommands</span>
|
||
|
||
<span class="c1"># [...]</span>
|
||
<span class="k">class</span> <span class="nc">CharacterCmdSet</span><span class="p">(</span><span class="n">default_cmds</span><span class="o">.</span><span class="n">CharacterCmdSet</span><span class="p">):</span>
|
||
<span class="c1"># [...]</span>
|
||
<span class="k">def</span> <span class="nf">at_cmdset_creation</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="c1"># [...]</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">movecommands</span><span class="o">.</span><span class="n">CmdExitErrorNorth</span><span class="p">())</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">movecommands</span><span class="o">.</span><span class="n">CmdExitErrorEast</span><span class="p">())</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">movecommands</span><span class="o">.</span><span class="n">CmdExitErrorSouth</span><span class="p">())</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">movecommands</span><span class="o">.</span><span class="n">CmdExitErrorWest</span><span class="p">())</span>
|
||
</pre></div>
|
||
</td></tr></table></div>
|
||
<p>After a <code class="docutils literal notranslate"><span class="pre">@reload</span></code> these commands (assuming you don’t get any errors - check your log) will be
|
||
loaded. What happens henceforth is that if you are in a room with an Exitobject (let’s say it’s
|
||
“north”), the proper Exit-command will overload your error command (also named “north”). But if you
|
||
enter an direction without having a matching exit for it, you will fallback to your default error
|
||
commands:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="o">></span> <span class="n">east</span>
|
||
<span class="n">You</span> <span class="n">cannot</span> <span class="n">move</span> <span class="n">east</span><span class="o">.</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Further expansions by the exit system (including manipulating the way the Exit command itself is
|
||
created) can be done by modifying the <a class="reference internal" href="../Components/Typeclasses.html"><span class="doc">Exit typeclass</span></a> directly.</p>
|
||
</div>
|
||
<div class="section" id="additional-comments">
|
||
<h2>Additional Comments<a class="headerlink" href="#additional-comments" title="Permalink to this headline">¶</a></h2>
|
||
<p>So why didn’t we create a single error command above? Something like this:</p>
|
||
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8</pre></div></td><td class="code"><div class="highlight"><pre><span></span> <span class="k">class</span> <span class="nc">CmdExitError</span><span class="p">(</span><span class="n">default_cmds</span><span class="o">.</span><span class="n">MuxCommand</span><span class="p">):</span>
|
||
<span class="s2">"Handles all exit-errors."</span>
|
||
<span class="n">key</span> <span class="o">=</span> <span class="s2">"error_cmd"</span>
|
||
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"north"</span><span class="p">,</span> <span class="s2">"n"</span><span class="p">,</span>
|
||
<span class="s2">"east"</span><span class="p">,</span> <span class="s2">"e"</span><span class="p">,</span>
|
||
<span class="s2">"south"</span><span class="p">,</span> <span class="s2">"s"</span><span class="p">,</span>
|
||
<span class="s2">"west"</span><span class="p">,</span> <span class="s2">"w"</span><span class="p">]</span>
|
||
<span class="c1">#[...]</span>
|
||
</pre></div>
|
||
</td></tr></table></div>
|
||
<p>The anwer is that this would <em>not</em> work and understanding why is important in order to not be
|
||
confused when working with commands and command sets.</p>
|
||
<p>The reason it doesn’t work is because Evennia’s <a class="reference internal" href="../Components/Commands.html"><span class="doc">command system</span></a> compares commands <em>both</em>
|
||
by <code class="docutils literal notranslate"><span class="pre">key</span></code> and by <code class="docutils literal notranslate"><span class="pre">aliases</span></code>. If <em>either</em> of those match, the two commands are considered <em>identical</em>
|
||
as far as cmdset merging system is concerned.</p>
|
||
<p>So the above example would work fine as long as there were no Exits at all in the room. But what
|
||
happens when we enter a room with an exit “north”? The Exit’s cmdset is merged onto the default one,
|
||
and since there is an alias match, the system determines our <code class="docutils literal notranslate"><span class="pre">CmdExitError</span></code> to be identical. It is
|
||
thus overloaded by the Exit command (which also correctly defaults to a higher priority). The result
|
||
is that you can go through the north exit normally but none of the error messages for the other
|
||
directions are available since the single error command was completely overloaded by the single
|
||
matching “north” exit-command.</p>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<div class="clearer"></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<p class="logo"><a href="../index.html">
|
||
<img class="logo" src="../_static/evennia_logo.png" alt="Logo"/>
|
||
</a></p>
|
||
<div id="searchbox" style="display: none" role="search">
|
||
<h3 id="searchlabel">Quick search</h3>
|
||
<div class="searchformwrapper">
|
||
<form class="search" action="../search.html" method="get">
|
||
<input type="text" name="q" aria-labelledby="searchlabel" />
|
||
<input type="submit" value="Go" />
|
||
</form>
|
||
</div>
|
||
</div>
|
||
<script>$('#searchbox').show(0);</script>
|
||
<p><h3><a href="../index.html">Table of Contents</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#">Default Exit Errors</a><ul>
|
||
<li><a class="reference internal" href="#adding-default-error-commands">Adding default error commands</a></li>
|
||
<li><a class="reference internal" href="#additional-comments">Additional Comments</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<div role="note" aria-label="source link">
|
||
<!--h3>This Page</h3-->
|
||
<ul class="this-page-menu">
|
||
<li><a href="../_sources/Howto/Default-Exit-Errors.md.txt"
|
||
rel="nofollow">Show Page Source</a></li>
|
||
</ul>
|
||
</div>
|
||
<h3>Versions</h3>
|
||
<ul>
|
||
<li><a href="Default-Exit-Errors.html">1.0-dev (develop branch)</a></li>
|
||
<li><a href="../../0.9.5/index.html">0.9.5 (master branch)</a></li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="General Index"
|
||
>index</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python Module Index"
|
||
>modules</a> |</li>
|
||
<li class="nav-item nav-item-0"><a href="../index.html">Evennia 1.0-dev</a> »</li>
|
||
<li class="nav-item nav-item-this"><a href="">Default Exit Errors</a></li>
|
||
</ul>
|
||
</div>
|
||
<div class="footer" role="contentinfo">
|
||
© Copyright 2020, The Evennia developer community.
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.
|
||
</div>
|
||
</body>
|
||
</html> |