<h1>NPCs reacting to your presence<aclass="headerlink"href="#npcs-reacting-to-your-presence"title="Permalink to this headline">¶</a></h1>
<divclass="highlight-none notranslate"><divclass="highlight"><pre><span></span>> north
------------------------------------
Meadow
You are standing in a green meadow.
A bandit is here.
------------------------------------
Bandit gives you a menacing look!
</pre></div>
</div>
<p>This tutorial shows the implementation of an NPC object that responds to characters entering their
location.</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>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># in mygame/typeclasses/npcs.py (for example)</span>
<p>Here we make a simple method on the <codeclass="docutils literal notranslate"><spanclass="pre">NPC</span></code>˙. We expect it to be called when a (player-)character enters the room. We don’t actually set the <codeclass="docutils literal notranslate"><spanclass="pre">is_aggressive</span></code><aclass="reference internal"href="../Components/Attributes.html"><spanclass="doc std std-doc">Attribute</span></a> beforehand; if it’s not set, the NPC is simply non-hostile.</p>
<p>Whenever <em>something</em> enters the <codeclass="docutils literal notranslate"><spanclass="pre">Room</span></code>, its <aclass="reference internal"href="../api/evennia.objects.objects.html#evennia.objects.objects.DefaultObject.at_object_receive"title="evennia.objects.objects.DefaultObject.at_object_receive"><spanclass="xref myst py py-meth">at_object_receive</span></a> hook will be called. So we should override it.</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># in mygame/typeclasses/rooms.py</span>
<p>Remember that Rooms are <codeclass="docutils literal notranslate"><spanclass="pre">Objects</span></code>. So the same <codeclass="docutils literal notranslate"><spanclass="pre">at_object_receive</span></code> hook will fire for you when you pick something up (making you ‘receive’ it). Or for a box when putting something inside it.</p>
</aside>
<p>A currently puppeted Character will have an <codeclass="docutils literal notranslate"><spanclass="pre">.account</span></code> attached to it. We use that to know that the thing arriving is a Character. We then use Evennia’s <aclass="reference internal"href="../api/evennia.utils.utils.html#evennia.utils.utils.inherits_from"title="evennia.utils.utils.inherits_from"><spanclass="xref myst py py-func">utils.inherits_from</span></a> helper utility to get every NPC in the room can each of their newly created <codeclass="docutils literal notranslate"><spanclass="pre">at_char_entered</span></code> method.</p>
<p>Make sure to <codeclass="docutils literal notranslate"><spanclass="pre">reload</span></code>.</p>
<p>Let’s create an NPC and make it aggressive. For the sake of this example, let’s assume your name is “Anna” and that there is a room to the north of your current location.</p>