evennia/docs/0.x/Default-Exit-Errors.html
2023-12-20 19:10:09 +01:00

232 lines
No EOL
16 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>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Default Exit Errors &#8212; Evennia 0.9.5 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>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"processClass": "tex2jax_process|mathjax_process|math|output_area"}})</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 0.9.5</a> &#187;</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">
<section class="tex2jax_ignore mathjax_ignore" 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 std std-doc">Exit Object</span></a>
and an <a class="reference internal" href="Commands.html"><span class="doc std std-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-none notranslate"><div class="highlight"><pre><span></span> &gt; jump out the window
Command &#39;jump out the window&#39; is not available. Type &quot;help&quot; for help.
</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-none 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>
<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 std std-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"><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="kc">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>
</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"><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>
</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-none notranslate"><div class="highlight"><pre><span></span> &gt; east
You cannot move east.
</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 std std-doc">Exit typeclass</span></a> directly.</p>
</section>
<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"><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>
</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 std std-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>
</section>
</section>
<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/Default-Exit-Errors.md.txt"
rel="nofollow">Show Page Source</a></li>
</ul>
</div><h3>Links</h3>
<ul>
<li><a href="https://www.evennia.com">Home page</a> </li>
<li><a href="https://github.com/evennia/evennia">Evennia Github</a> </li>
<li><a href="http://games.evennia.com">Game Index</a> </li>
<li><a href="http://webchat.freenode.net/?channels=evennia&uio=MT1mYWxzZSY5PXRydWUmMTE9MTk1JjEyPXRydWUbb">IRC</a> -
<a href="https://discord.gg/NecFePw">Discord</a> -
<a href="https://groups.google.com/forum/#%21forum/evennia">Forums</a>
</li>
<li><a href="http://evennia.blogspot.com/">Evennia Dev blog</a> </li>
</ul>
<h3>Versions</h3>
<ul>
<li><a href="../1.0-dev/index.html">1.0-dev (develop branch)</a></li>
<li><a href="Default-Exit-Errors.html">0.9.5 (v0.9.5 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 0.9.5</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">Default Exit Errors</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2020, The Evennia developer community.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.
</div>
</body>
</html>