evennia/docs/1.0-dev/Default-Exit-Errors.html
2020-06-13 12:23:41 +02:00

233 lines
No EOL
15 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Default Exit Errors &#8212; Evennia 1.0-dev documentation</title>
<link rel="stylesheet" href="_static/alabaster.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="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<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="Objects.html"><span class="doc">Exit Object</span></a> and an <a class="reference internal" href="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 doesnt care about the difference:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="o">&gt;</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">&#39;jump out the window&#39;</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">&quot;help&quot;</span> <span class="k">for</span> <span class="n">help</span><span class="o">.</span>
</pre></div>
</div>
<p>Many games dont need this type of freedom however. They define only the cardinal directions as valid exit names (Evennias <code class="docutils literal notranslate"><span class="pre">&#64;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> &gt; west
Command &#39;west&#39; is not available. Maybe you meant &quot;@set&quot; or &quot;@reset&quot;?
</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 couldnt 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="Adding-Command-Tutorial.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 well 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">&quot;Parent class for all exit-errors.&quot;</span>
<span class="n">locks</span> <span class="o">=</span> <span class="s2">&quot;cmd:all()&quot;</span>
<span class="n">arg_regex</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">&quot;\s|$&quot;</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">&quot;returns the error&quot;</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">&quot;You cannot move </span><span class="si">%s</span><span class="s2">.&quot;</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">&quot;north&quot;</span>
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;n&quot;</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">&quot;east&quot;</span>
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;e&quot;</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">&quot;south&quot;</span>
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;s&quot;</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">&quot;west&quot;</span>
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;w&quot;</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">&#64;reload</span></code> these commands (assuming you dont get any errors - check your log) will be loaded. What happens henceforth is that if you are in a room with an Exitobject (lets say its “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">&gt;</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="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 didnt 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">&quot;Handles all exit-errors.&quot;</span>
<span class="n">key</span> <span class="o">=</span> <span class="s2">&quot;error_cmd&quot;</span>
<span class="n">aliases</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;north&quot;</span><span class="p">,</span> <span class="s2">&quot;n&quot;</span><span class="p">,</span>
<span class="s2">&quot;east&quot;</span><span class="p">,</span> <span class="s2">&quot;e&quot;</span><span class="p">,</span>
<span class="s2">&quot;south&quot;</span><span class="p">,</span> <span class="s2">&quot;s&quot;</span><span class="p">,</span>
<span class="s2">&quot;west&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</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 doesnt work is because Evennias <a class="reference internal" href="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 Exits 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>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<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 class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<!--h3>This Page</h3-->
<ul class="this-page-menu">
<li><a href="_sources/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.1/index.html">0.9.1 (master branch)</a></li>
</ul>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2020, The Evennia developer community.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 2.4.4</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
|
<a href="_sources/Default-Exit-Errors.md.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>