<h1>NPCs reacting to your presence<aclass="headerlink"href="#npcs-reacting-to-your-presence"title="Link to this heading">¶</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-ref">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>
<pclass="sidebar-title">Passing extra information</p>
<p>Note that we don’t use the <codeclass="docutils literal notranslate"><spanclass="pre">**kwargs</span></code> property here. This can be used to pass extra information into hooks in your game and would be used when you make custom move commands. For example, if you <codeclass="docutils literal notranslate"><spanclass="pre">run</span></code> into the room, you could inform all hooks by doing <codeclass="docutils literal notranslate"><spanclass="pre">obj.move_to(...,</span><spanclass="pre">running=True)</span></code>. Maybe your librarian NPC should have a separate reaction for people running into their library!</p>
<p>We make sure to pass the <codeclass="docutils literal notranslate"><spanclass="pre">**kwargs</span></code> from the standard <codeclass="docutils literal notranslate"><spanclass="pre">at_object_receive</span></code> hook below.</p>
</aside>
<p>Here we make a simple method on the <codeclass="docutils literal notranslate"><spanclass="pre">NPC</span></code>˙ called <codeclass="docutils literal notranslate"><spanclass="pre">at_char_entered</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="std std-doc">Attribute</span></a> beforehand; we leave this up for the admin to activate in-game. 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.commands.default.general.html#evennia.commands.default.general.DefaultObject.at_object_receive"title="evennia.commands.default.general.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>, and other Objects have these same hooks. So an <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, for example.</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>