<h1>Tutorial Aggressive NPCs<aclass="headerlink"href="#tutorial-aggressive-npcs"title="Permalink to this headline">¶</a></h1>
<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="../Components/Scripts.html"><spanclass="doc std std-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 std std-doc">Basic Game tutorial</span></a> if you haven’t already done this.</p>
<p>What we will need is the following:</p>
<ulclass="simple">
<li><p>An NPC typeclass that can react when someone enters.</p></li>
<li><p>A custom <aclass="reference internal"href="../Components/Objects.html#rooms"><spanclass="std std-doc">Room</span></a> typeclass that can tell the NPC that someone entered.</p></li>
<li><p>We will also tweak our default <codeclass="docutils literal notranslate"><spanclass="pre">Character</span></code> typeclass a little.</p></li>
</ul>
<p>To begin with, we need to create an NPC typeclass. Create a new file inside of your typeclasses
folder and name it <codeclass="docutils literal notranslate"><spanclass="pre">npcs.py</span></code> and then add the following code:</p>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">execute_cmd</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"say Graaah, die </span><spanclass="si">{</span><spanclass="n">character</span><spanclass="si">}</span><spanclass="s2">!"</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
quest-giving) as your game design dictates.</p>
<p>Now your <codeclass="docutils literal notranslate"><spanclass="pre">typeclasses.rooms</span></code> module needs to have the following added:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># Add this import to the top of your file.</span>
<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>
</div></blockquote>
<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_post_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
<divclass="highlight-python notranslate"><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>
</div></blockquote>
<p>When you enter the aggressive NPC’s location, it will default to using its peaceful action (say your