<li><aclass="reference internal"href="#preventing-character-from-moving-based-on-a-condition">Preventing character from moving based on a condition</a></li>
<li><aclass="reference internal"href="#reference-initiating-object-in-an-evmenu-command">Reference initiating object in an EvMenu command.</a></li>
<li><aclass="reference internal"href="#adding-color-to-default-evennia-channels">Adding color to default Evennia Channels</a></li>
<li><aclass="reference internal"href="#selectively-turn-off-commands-in-a-room">Selectively turn off commands in a room</a></li>
<li><aclass="reference internal"href="#select-command-based-on-a-condition">Select Command based on a condition</a></li>
<li><aclass="reference internal"href="#automatically-updating-code-when-reloading">Automatically updating code when reloading</a></li>
<li><aclass="reference internal"href="#changing-all-exit-messages">Changing all exit messages</a></li>
<li><aclass="reference internal"href="#add-parsing-with-the-to-delimiter">Add parsing with the “to” delimiter</a></li>
<li><aclass="reference internal"href="#store-last-used-session-ip-address">Store last used session IP address</a></li>
<li><aclass="reference internal"href="#non-latin-characters-in-evtable">Non-latin characters in EvTable</a></li>
<h1>Coding FAQ<aclass="headerlink"href="#coding-faq"title="Permalink to this headline">¶</a></h1>
<p><em>This FAQ page is for users to share their solutions to coding problems. Keep it brief and link to the docs if you can rather than too lengthy explanations. Don’t forget to check if an answer already exists before answering - maybe you can clarify that answer rather than to make a new Q&A section.</em></p>
<divclass="section"id="table-of-contents">
<h2>Table of Contents<aclass="headerlink"href="#table-of-contents"title="Permalink to this headline">¶</a></h2>
<li><p><aclass="reference external"href="/Coding-FAQ.html#preventing-character-from-moving-based-on-a-condition">Preventing character from moving based on a condition</a></p></li>
<li><p><aclass="reference external"href="/Coding-FAQ.html#reference-initiating-object-in-an-evmenu-command">Reference initiating object in an EvMenu command</a></p></li>
<li><p><aclass="reference external"href="/Coding-FAQ.html#adding-color-to-default-evennia-channels">Adding color to default Evennia Channels</a></p></li>
<li><p><aclass="reference external"href="/Coding-FAQ.html#selectively-turn-off-commands-in-a-room">Selectively turn off commands in a room</a></p></li>
<li><p><aclass="reference external"href="/Coding-FAQ.html#select-command-based-on-a-condition">Select Command based on a condition</a></p></li>
<li><p><aclass="reference external"href="/Coding-FAQ.html#automatically-updating-code-when-reloading">Automatically updating code when reloading</a></p></li>
<li><p><aclass="reference external"href="/Coding-FAQ.html#changing-all-exit-messages">Changing all exit messages</a></p></li>
<li><p><aclass="reference external"href="/Coding-FAQ.html#add-parsing-with-the-to-delimiter">Add parsing with the “to” delimiter</a></p></li>
<li><p><aclass="reference external"href="/Coding-FAQ.html#store-last-used-session-ip-address">Store last used session IP address</a></p></li>
<li><p><aclass="reference external"href="/Coding-FAQ.html#non-latin-characters-in-evtable">Use wide characters with EvTable</a></p></li>
<h2>Removing default commands<aclass="headerlink"href="#removing-default-commands"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> How does one <em>remove</em> (not replace) e.g. the default <codeclass="docutils literal notranslate"><spanclass="pre">get</span></code><aclass="reference internal"href="Commands.html"><spanclass="doc">Command</span></a> from the Character <aclass="reference internal"href="Command-Sets.html"><spanclass="doc">Command Set</span></a>?</p>
<p><strong>A:</strong> Go to <codeclass="docutils literal notranslate"><spanclass="pre">mygame/commands/default_cmdsets.py</span></code>. Find the <codeclass="docutils literal notranslate"><spanclass="pre">CharacterCmdSet</span></code> class. It has one method named <codeclass="docutils literal notranslate"><spanclass="pre">at_cmdset_creation</span></code>. At the end of that method, add the following line: <codeclass="docutils literal notranslate"><spanclass="pre">self.remove(default_cmds.CmdGet())</span></code>. See the <aclass="reference internal"href="Adding-Command-Tutorial.html"><spanclass="doc">Adding Commands Tutorial</span></a> for more info.</p>
<h2>Preventing character from moving based on a condition<aclass="headerlink"href="#preventing-character-from-moving-based-on-a-condition"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> How does one keep a character from using any exit, if they meet a certain condition? (I.E. in combat, immobilized, etc.)</p>
<p><strong>A:</strong> The <codeclass="docutils literal notranslate"><spanclass="pre">at_before_move</span></code> hook is called by Evennia just before performing any move. If it returns <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>, the move is aborted. Let’s say we want to check for an <aclass="reference internal"href="Attributes.html"><spanclass="doc">Attribute</span></a><codeclass="docutils literal notranslate"><spanclass="pre">cantmove</span></code>. Add the following code to the <codeclass="docutils literal notranslate"><spanclass="pre">Character</span></code> class:</p>
<spanclass="s2">"Called just before trying to move"</span>
<spanclass="k">if</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">db</span><spanclass="o">.</span><spanclass="n">cantmove</span><spanclass="p">:</span><spanclass="c1"># replace with condition you want to test</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"Something is preventing you from moving!"</span><spanclass="p">)</span>
<h2>Reference initiating object in an EvMenu command.<aclass="headerlink"href="#reference-initiating-object-in-an-evmenu-command"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> An object has a Command on it starts up an EvMenu instance. How do I capture a reference to that object for use in the menu?</p>
<p><strong>A:</strong> When an <aclass="reference internal"href="EvMenu.html"><spanclass="doc">EvMenu</span></a> is started, the menu object is stored as <codeclass="docutils literal notranslate"><spanclass="pre">caller.ndb._menutree</span></code>. This is a good place to store menu-specific things since it will clean itself up when the menu closes. When initiating the menu, any additional keywords you give will be available for you as properties on this menu object:</p>
<p>Inside the menu you can now access the object through <codeclass="docutils literal notranslate"><spanclass="pre">caller.ndb._menutree.stored_obj</span></code>.</p>
<h2>Adding color to default Evennia Channels<aclass="headerlink"href="#adding-color-to-default-evennia-channels"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> How do I add colors to the names of Evennia channels?</p>
<p><strong>A:</strong> The Channel typeclass’<codeclass="docutils literal notranslate"><spanclass="pre">channel_prefix</span></code> method decides what is shown at the beginning of a channel send. Edit <codeclass="docutils literal notranslate"><spanclass="pre">mygame/typeclasses/channels.py</span></code> (and then <codeclass="docutils literal notranslate"><spanclass="pre">@reload</span></code>):</p>
<p>Additional hint: To make colors easier to change from one place you could instead put the <codeclass="docutils literal notranslate"><spanclass="pre">CHANNEL_COLORS</span></code> dict in your settings file and import it as <codeclass="docutils literal notranslate"><spanclass="pre">from</span><spanclass="pre">django.conf.settings</span><spanclass="pre">import</span><spanclass="pre">CHANNEL_COLORS</span></code>.</p>
<h2>Selectively turn off commands in a room<aclass="headerlink"href="#selectively-turn-off-commands-in-a-room"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> I want certain commands to turn off in a given room. They should still work normally for staff.</p>
<p><strong>A:</strong> This is done using a custom cmdset on a room <aclass="reference internal"href="Locks.html"><spanclass="doc">locked with the ‘call’ lock type</span></a>. Only if this lock is passed will the commands on the room be made available to an object inside it. Here is an example of a room where certain commands are disabled for non-staff:</p>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"You cannot do that in this room."</span><spanclass="p">)</span>
<p>After <codeclass="docutils literal notranslate"><spanclass="pre">@reload</span></code>, make some <codeclass="docutils literal notranslate"><spanclass="pre">BlockingRooms</span></code> (or switch a room to it with <codeclass="docutils literal notranslate"><spanclass="pre">@typeclass</span></code>). Entering one will now replace the given commands for anyone that does not have the <codeclass="docutils literal notranslate"><spanclass="pre">Builders</span></code> or higher permission. Note that the ‘call’ lock is special in that even the superuser will be affected by it (otherwise superusers would always see other player’s cmdsets and a game would be unplayable for superusers).</p>
<h2>Select Command based on a condition<aclass="headerlink"href="#select-command-based-on-a-condition"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> I want a command to be available only based on a condition. For example I want the “werewolf” command to only be available on a full moon, from midnight to three in-game time.</p>
<p><strong>A:</strong> This is easiest accomplished by putting the “werewolf” command on the Character as normal, but to <aclass="reference internal"href="Locks.html"><spanclass="doc">lock</span></a> it with the “cmd” type lock. Only if the “cmd” lock type is passed will the command be available.</p>
<p>Add this to the <aclass="reference internal"href="Adding-Command-Tutorial.html"><spanclass="doc">default cmdset as usual</span></a>. The <codeclass="docutils literal notranslate"><spanclass="pre">is_full_moon</span></code><aclass="reference external"href="/Locks.html#lock-functions">lock function</a> does not yet exist. We must create that:</p>
<spanclass="c1"># calculate if the moon is full here and</span>
<spanclass="c1"># if current game time is between starthour and endhour</span>
<spanclass="c1"># return True or False</span>
</pre></div>
</td></tr></table></div>
<p>After a <codeclass="docutils literal notranslate"><spanclass="pre">@reload</span></code>, the <codeclass="docutils literal notranslate"><spanclass="pre">werewolf</span></code> command will be available only at the right time, that is when the <codeclass="docutils literal notranslate"><spanclass="pre">is_full_moon</span></code> lock function returns True.</p>
<h2>Automatically updating code when reloading<aclass="headerlink"href="#automatically-updating-code-when-reloading"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> I have a development server running Evennia. Can I have the server update its code-base when I reload?</p>
<p><strong>A:</strong> Having a development server that pulls updated code whenever you reload it can be really useful if you have limited shell access to your server, or want to have it done automatically. If you have your project in a configured Git environment, it’s a matter of automatically calling <codeclass="docutils literal notranslate"><spanclass="pre">git</span><spanclass="pre">pull</span></code> when you reload. And that’s pretty straightforward:</p>
<p>That’s all. We call <codeclass="docutils literal notranslate"><spanclass="pre">subprocess</span></code> to execute a shell command (that code works on Windows and Linux, assuming the current directory is your game directory, which is probably the case when you run Evennia). <codeclass="docutils literal notranslate"><spanclass="pre">call</span></code> waits for the process to complete, because otherwise, Evennia would reload on partially-modified code, which would be problematic.</p>
<p>Now, when you enter <codeclass="docutils literal notranslate"><spanclass="pre">@reload</span></code> on your development server, the game repository is updated from the configured remote repository (Github, for instance). Your development cycle could resemble something like:</p>
<olclass="simple">
<li><p>Coding on the local machine.</p></li>
<li><p>Testing modifications.</p></li>
<li><p>Committing once, twice or more (being sure the code is still working, unittests are pretty useful here).</p></li>
<li><p>When the time comes, login to the development server and run <codeclass="docutils literal notranslate"><spanclass="pre">@reload</span></code>.</p></li>
</ol>
<p>The reloading might take one or two additional seconds, since Evennia will pull from your remote Git repository. But it will reload on it and you will have your modifications ready, without needing connecting to your server using SSH or something similar.</p>
<h2>Changing all exit messages<aclass="headerlink"href="#changing-all-exit-messages"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> How can I change the default exit messages to something like “XXX leaves east” or “XXX arrives from the west”?</p>
<p><strong>A:</strong> the default exit messages are stored in two hooks, namely <codeclass="docutils literal notranslate"><spanclass="pre">announce_move_from</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">announce_move_to</span></code>, on the <codeclass="docutils literal notranslate"><spanclass="pre">Character</span></code> typeclass (if what you want to change is the message other characters will see when a character exits).</p>
<p>These two hooks provide some useful features to easily update the message to be displayed. They take both the default message and mapping as argument. You can easily call the parent hook with these information:</p>
<ulclass="simple">
<li><p>The message represents the string of characters sent to characters in the room when a character leaves.</p></li>
<li><p>The mapping is a dictionary containing additional mappings (you will probably not need it for simple customization).</p></li>
</ul>
<p>It is advisable to look in the <aclass="reference external"href="https://github.com/evennia/evennia/tree/master/evennia/objects/objects.py">code of both hooks</a>, and read the hooks’ documentation. The explanations on how to quickly update the message are shown below:</p>
<spanclass="sd"> You can override this method and call its parent with a</span>
<spanclass="sd"> message to simply change the default message. In the string,</span>
<spanclass="sd"> you can use the following as mappings (between braces):</span>
<spanclass="sd"> object: the object which is moving.</span>
<spanclass="sd"> exit: the exit from which the object is moving (if found).</span>
<spanclass="sd"> origin: the location of the object before the move.</span>
<spanclass="sd"> destination: the location of the object after moving.</span>
<spanclass="sd">"""</span>
<spanclass="nb">super</span><spanclass="p">()</span><spanclass="o">.</span><spanclass="n">announce_move_to</span><spanclass="p">(</span><spanclass="n">source_location</span><spanclass="p">,</span><spanclass="n">msg</span><spanclass="o">=</span><spanclass="s2">"{object} arrives from the {exit}."</span><spanclass="p">)</span>
</pre></div>
</td></tr></table></div>
<p>We override both hooks, but call the parent hook to display a different message. If you read the provided docstrings, you will better understand why and how we use mappings (information between braces). You can provide additional mappings as well, if you want to set a verb to move, for instance, or other, extra information.</p>
<h2>Add parsing with the “to” delimiter<aclass="headerlink"href="#add-parsing-with-the-to-delimiter"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> How do I change commands to undestand say <codeclass="docutils literal notranslate"><spanclass="pre">give</span><spanclass="pre">obj</span><spanclass="pre">to</span><spanclass="pre">target</span></code> as well as the default <codeclass="docutils literal notranslate"><spanclass="pre">give</span><spanclass="pre">obj</span><spanclass="pre">=</span><spanclass="pre">target</span></code>?</p>
<p><strong>A:</strong> You can make change the default <codeclass="docutils literal notranslate"><spanclass="pre">MuxCommand</span></code> parent with your own class making a small change in its <codeclass="docutils literal notranslate"><spanclass="pre">parse</span></code> method:</p>
<spanclass="k">if</span><spanclass="s2">" to "</span><spanclass="ow">in</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">args</span><spanclass="p">:</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">lhs</span><spanclass="p">,</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">rhs</span><spanclass="o">=</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">args</span><spanclass="o">.</span><spanclass="n">split</span><spanclass="p">(</span><spanclass="s2">" to "</span><spanclass="p">,</span><spanclass="mi">1</span><spanclass="p">)</span>
</pre></div>
</td></tr></table></div>
<p>Next you change the parent of the default commands in settings:</p>
<p>Do a <codeclass="docutils literal notranslate"><spanclass="pre">@reload</span></code> and all default commands will now use your new tweaked parent class. A copy of the
MuxCommand class is also found commented-out in the <codeclass="docutils literal notranslate"><spanclass="pre">mygame/commands/command.py</span></code> file.</p>
<h2>Store last used session IP address<aclass="headerlink"href="#store-last-used-session-ip-address"title="Permalink to this headline">¶</a></h2>
<p><strong>Q:</strong> If a user has already logged out of an Evennia account, their IP is no longer visible to staff that wants to ban-by-ip (instead of the user) with <codeclass="docutils literal notranslate"><spanclass="pre">@ban/ip</span></code>?</p>
<p><strong>A:</strong> One approach is to write the IP from the last session onto the “account” account object.</p>
<p>Adding timestamp for login time and appending to a list to keep the last N login IP addresses and timestamps is possible, also. Additionally, if you don’t want the list to grow beyond a <codeclass="docutils literal notranslate"><spanclass="pre">do_not_exceed</span></code> length, conditionally pop a value after you’ve added it, if the length has grown too long.</p>
<p><strong>NOTE:</strong> You’ll need to add <codeclass="docutils literal notranslate"><spanclass="pre">import</span><spanclass="pre">time</span></code> to generate the login timestamp.</p>
<spanclass="n">do_not_exceed</span><spanclass="o">=</span><spanclass="mi">24</span><spanclass="c1"># Keep the last two dozen entries</span>
<spanclass="n">session</span><spanclass="o">=</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">sessions</span><spanclass="o">.</span><spanclass="n">all</span><spanclass="p">()[</span><spanclass="o">-</span><spanclass="mi">1</span><spanclass="p">]</span><spanclass="c1"># Most recent session</span>
<p>This only stores the data. You may want to interface the <codeclass="docutils literal notranslate"><spanclass="pre">@ban</span></code> command or make a menu-driven viewer for staff to browse the list and display how long ago the login occurred.</p>
<p><strong>A:</strong> The reason for this is because certain non-latin characters are <em>visually</em> much wider than their len() suggests. There is little Evennia can (reliably) do about this. If you are using such characters, you need to make sure to use a suitable mono-spaced font where are width are equal. You can set this in your web client and need to recommend it for telnet-client users. See <aclass="reference external"href="https://github.com/evennia/evennia/issues/1522">this discussion</a> where some suitable fonts are suggested.</p>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> and ❤️ using a custom <ahref="https://github.com/LinxiFan/Sphinx-theme">theme</a> based on <ahref="https://readthedocs.org">Read the Docs</a>.