<p>This tutorial shows the implementation of an NPC object that responds to characters entering their
location. In this example the NPC has the option to respond aggressively or not, but any actions
could be triggered this way.</p>
<p>One could imagine using a <aclass="reference internal"href="Scripts.html"><spanclass="doc">Script</span></a> that is constantly checking for newcomers. This would be
highly inefficient (most of the time its check would fail). Instead we handle this on-demand by
using a couple of existing object hooks to inform the NPC that a Character has entered.</p>
<p>It is assumed that you already know how to create custom room and character typeclasses, please see
the <aclass="reference internal"href="Tutorial-for-basic-MUSH-like-game.html"><spanclass="doc">Basic Game tutorial</span></a> if you haven’t already done this.</p>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">execute_cmd</span><spanclass="p">(</span><spanclass="n">f</span><spanclass="s2">"say Graaah, die {character}!"</span><spanclass="p">)</span>
<p>We will define our custom <codeclass="docutils literal notranslate"><spanclass="pre">Character</span></code> typeclass below. As for the new <codeclass="docutils literal notranslate"><spanclass="pre">at_char_entered</span></code> method we’ve
just defined, we’ll ensure that it will be called by the room where the NPC is located, when a
player enters that room. You’ll notice that right now, the NPC merely speaks. You can expand this
part as you like and trigger all sorts of effects here (like combat code, fleeing, bartering or
<spanclass="k">if</span><spanclass="n">utils</span><spanclass="o">.</span><spanclass="n">inherits_from</span><spanclass="p">(</span><spanclass="n">obj</span><spanclass="p">,</span><spanclass="s1">'typeclasses.npcs.NPC'</span><spanclass="p">):</span><spanclass="c1"># An NPC has entered</span>
<p><codeclass="docutils literal notranslate"><spanclass="pre">inherits_from</span></code> must be given the full path of the class. If the object inherited a class from your
<codeclass="docutils literal notranslate"><spanclass="pre">world.races</span></code> module, then you would check inheritance with <codeclass="docutils literal notranslate"><spanclass="pre">world.races.Human</span></code>, for example. There
is no need to import these prior, as we are passing in the full path. As a matter of a fact,
<codeclass="docutils literal notranslate"><spanclass="pre">inherits_from</span></code> does not properly work if you import the class and only pass in the name of the
is a default hook of the <codeclass="docutils literal notranslate"><spanclass="pre">DefaultObject</span></code> typeclass (and its children). Here we are overriding this
hook in our customized room typeclass to suit our needs.</p>
<p>This room checks the typeclass of objects entering it (using <codeclass="docutils literal notranslate"><spanclass="pre">utils.inherits_from</span></code> and responds to
<codeclass="docutils literal notranslate"><spanclass="pre">Characters</span></code>, ignoring other NPCs or objects. When triggered the room will look through its
contents and inform any <codeclass="docutils literal notranslate"><spanclass="pre">NPCs</span><spanclass="pre">inside</span><spanclass="pre">by</span><spanclass="pre">calling</span><spanclass="pre">their</span></code>at_char_entered` method.</p>
<p>You’ll also see that we have added a ‘look’ into this code. This is because, by default, the
<codeclass="docutils literal notranslate"><spanclass="pre">at_object_receive</span></code> is carried out <em>before</em> the character’s <codeclass="docutils literal notranslate"><spanclass="pre">at_after_move</span></code> which, we will now
overload. This means that a character entering would see the NPC perform its actions before the
‘look’ command. Deactivate the look command in the default <codeclass="docutils literal notranslate"><spanclass="pre">Character</span></code> class within the
8</pre></div></td><tdclass="code"><divclass="highlight"><pre><span></span><spanclass="c1"># Add this hook in any blank area within your Character class.</span>
<div><p>Note: You could also give the path as <codeclass="docutils literal notranslate"><spanclass="pre">typeclasses.npcs.NPC</span></code>, but Evennia will look into the
<codeclass="docutils literal notranslate"><spanclass="pre">typeclasses</span></code> folder automatically, so this is a little shorter.</p>