<pclass="last">You are reading an old version of the Evennia documentation. <ahref="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.
<h1>Return custom errors on missing Exits<aclass="headerlink"href="#return-custom-errors-on-missing-exits"title="Permalink to this headline">¶</a></h1>
<divclass="highlight-none notranslate"><divclass="highlight"><pre><span></span>> north
Ouch! You bump into a wall!
> out
But you are already outside ...?
</pre></div>
</div>
<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 <aclass="reference internal"href="../Components/Objects.html"><spanclass="doc std std-doc">Exit Object</span></a> and
an <aclass="reference internal"href="../Components/Commands.html"><spanclass="doc std std-doc">Exit Command</span></a> stored on said exit object. The command has the same key and aliases as the
exit-object, which is why you can see the exit in the room and just write its name to traverse it.</p>
<p>So if you try to enter the name of a non-existing exit, Evennia treats is the same way as if you were trying to use a non-existing command:</p>
<divclass="highlight-none notranslate"><divclass="highlight"><pre><span></span>> jump out the window
Command 'jump out the window' is not available. Type "help" for help.
</pre></div>
</div>
<p>Many games don’t need this type of freedom. They define only the cardinal directions as valid exit names ( Evennia’s <codeclass="docutils literal notranslate"><spanclass="pre">tunnel</span></code> command also offers this functionality). In this case, the error starts to look less logical:</p>
<divclass="highlight-none notranslate"><divclass="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>
<divclass="highlight-none notranslate"><divclass="highlight"><pre><span></span>> west
You cannot move west.
</pre></div>
</div>
<p>The way to do this is to give Evennia an <em>alternative</em> Command to use when no Exit-Command is found in the room. See <aclass="reference internal"href="Beginner-Tutorial/Part1/Beginner-Tutorial-Adding-Commands.html"><spanclass="doc std std-doc">Adding Commands</span></a> for more info about the process of adding new Commands to Evennia.</p>
<p>In this example we will just echo an error message, but you could do everything (maybe you lose health if you bump into a wall?)</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># for example in a file mygame/commands/movecommands.py</span>
<p>We pack our commands in a new little cmdset; if we add this to our <codeclass="docutils literal notranslate"><spanclass="pre">CharacterCmdSet</span></code>, we can just add more errors to <codeclass="docutils literal notranslate"><spanclass="pre">MovementFailCmdSet</span></code> later without having to change code in two places.</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># in mygame/commands/default_cmdsets.py</span>
<p><codeclass="docutils literal notranslate"><spanclass="pre">reload</span></code> the server. 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 <em>overload</em> your error command (also named “north”). But if you enter a direction without having a matching exit for it, you will fall back to your default error commands:</p>
<divclass="highlight-none notranslate"><divclass="highlight"><pre><span></span>> 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 <aclass="reference internal"href="../Components/Typeclasses.html"><spanclass="doc std std-doc">Exit typeclass</span></a> directly.</p>
<sectionid="why-not-a-single-command">
<h2>Why not a single command?<aclass="headerlink"href="#why-not-a-single-command"title="Permalink to this headline">¶</a></h2>
<p>So why didn’t we create a single error command above? Something like this:</p>
<p>This would <em>not</em> work the way we want. Understanding why is important.</p>
<p>Evennia’s <aclass="reference internal"href="../Components/Commands.html"><spanclass="doc std std-doc">command system</span></a> compares commands by key and/or aliases. If <em>any</em> key or alias match, the two commands are considered <em>identical</em>. When the cmdsets merge, priority will then decide which of these ‘identical’ commandss replace which.</p>
<p>So the above example would work fine as long as there were <em>no Exits at all</em> in the room. But when we enter a room with an exit “north”, its Exit-command (which has a higher priority) will override the single <codeclass="docutils literal notranslate"><spanclass="pre">CmdExitError</span></code> with its alias ‘north’. So the <codeclass="docutils literal notranslate"><spanclass="pre">CmdExitError</span></code> will be gone and while “north” will work, we’ll again get the normal “Command not recognized” error for the other directions.</p>
<pclass="last">You are reading an old version of the Evennia documentation. <ahref="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.