2022-11-21 23:18:31 +00:00
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" / > < meta name = "generator" content = "Docutils 0.17.1: http://docutils.sourceforge.net/" / >
2023-10-19 20:22:27 +00:00
< title > NPCs reacting to your presence — Evennia 2.x documentation< / title >
2022-11-21 23:18:31 +00:00
< link rel = "stylesheet" href = "../_static/nature.css" type = "text/css" / >
< link rel = "stylesheet" href = "../_static/pygments.css" type = "text/css" / >
< script id = "documentation_options" data-url_root = "../" src = "../_static/documentation_options.js" > < / script >
< script src = "../_static/jquery.js" > < / script >
< script src = "../_static/underscore.js" > < / script >
< script src = "../_static/doctools.js" > < / script >
< script src = "../_static/language_data.js" > < / script >
< link rel = "shortcut icon" href = "../_static/favicon.ico" / >
< link rel = "index" title = "Index" href = "../genindex.html" / >
< link rel = "search" title = "Search" href = "../search.html" / >
< link rel = "next" title = "NPC merchants" href = "Tutorial-NPC-Merchants.html" / >
< link rel = "prev" title = "NPCs that listen to what is said" href = "Tutorial-NPC-Listening.html" / >
< / head > < body >
2023-10-19 20:22:27 +00:00
2022-11-21 23:18:31 +00:00
< div class = "related" role = "navigation" aria-label = "related navigation" >
< h3 > Navigation< / h3 >
< ul >
< li class = "right" style = "margin-right: 10px" >
< a href = "../genindex.html" title = "General Index"
accesskey="I">index< / a > < / li >
< li class = "right" >
< a href = "../py-modindex.html" title = "Python Module Index"
>modules< / a > |< / li >
< li class = "right" >
< a href = "Tutorial-NPC-Merchants.html" title = "NPC merchants"
accesskey="N">next< / a > |< / li >
< li class = "right" >
< a href = "Tutorial-NPC-Listening.html" title = "NPCs that listen to what is said"
accesskey="P">previous< / a > |< / li >
2023-10-19 20:22:27 +00:00
< li class = "nav-item nav-item-0" > < a href = "../index.html" > Evennia 2.x< / a > » < / li >
2023-12-20 18:20:52 +01:00
< li class = "nav-item nav-item-1" > < a href = "Howtos-Overview.html" accesskey = "U" > Tutorials and Howto’ s< / a > » < / li >
2022-11-21 23:18:31 +00:00
< li class = "nav-item nav-item-this" > < a href = "" > NPCs reacting to your presence< / a > < / li >
< / ul >
2023-12-20 18:20:52 +01:00
< / div >
2022-11-21 23:18:31 +00:00
< div class = "document" >
< div class = "documentwrapper" >
< div class = "sphinxsidebar" role = "navigation" aria-label = "main navigation" >
< div class = "sphinxsidebarwrapper" >
< p class = "logo" > < a href = "../index.html" >
< img class = "logo" src = "../_static/evennia_logo.png" alt = "Logo" / >
< / a > < / p >
< div id = "searchbox" style = "display: none" role = "search" >
< h3 id = "searchlabel" > Quick search< / h3 >
< div class = "searchformwrapper" >
< form class = "search" action = "../search.html" method = "get" >
< input type = "text" name = "q" aria-labelledby = "searchlabel" / >
< input type = "submit" value = "Go" / >
< / form >
< / div >
< / div >
< script > $ ( '#searchbox' ) . show ( 0 ) ; < / script >
< h4 > Previous topic< / h4 >
< p class = "topless" > < a href = "Tutorial-NPC-Listening.html"
title="previous chapter">NPCs that listen to what is said< / a > < / p >
< h4 > Next topic< / h4 >
< p class = "topless" > < a href = "Tutorial-NPC-Merchants.html"
title="next chapter">NPC merchants< / a > < / p >
< div role = "note" aria-label = "source link" >
<!-- h3>This Page</h3 -->
< ul class = "this-page-menu" >
< li > < a href = "../_sources/Howtos/Tutorial-NPC-Reacting.md.txt"
rel="nofollow">Show Page Source< / a > < / li >
< / ul >
< / div > < h3 > Links< / h3 >
< ul >
2023-10-19 20:22:27 +00:00
< li > < a href = "https://www.evennia.com/docs/latest/index.html" > Documentation Top< / a > < / li >
< li > < a href = "https://www.evennia.com" > Evennia Home< / a > < / li >
< li > < a href = "https://github.com/evennia/evennia" > Github< / a > < / li >
2022-11-21 23:18:31 +00:00
< li > < a href = "http://games.evennia.com" > Game Index< / a > < / li >
< li >
< a href = "https://discord.gg/AJJpcRUhtF" > Discord< / a > -
< a href = "https://github.com/evennia/evennia/discussions" > Discussions< / a > -
< a href = "https://evennia.blogspot.com/" > Blog< / a >
< / li >
< / ul >
< / div >
< / div >
< div class = "bodywrapper" >
< div class = "body" role = "main" >
< section class = "tex2jax_ignore mathjax_ignore" id = "npcs-reacting-to-your-presence" >
< h1 > NPCs reacting to your presence< a class = "headerlink" href = "#npcs-reacting-to-your-presence" title = "Permalink to this headline" > ¶< / a > < / h1 >
< div class = "highlight-none notranslate" > < div class = "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 >
< ul class = "simple" >
< li > < p > An NPC typeclass that can react when someone enters.< / p > < / li >
2023-10-19 20:22:27 +00:00
< li > < p > A custom < span class = "xref myst" > Room< / span > typeclass that can tell the NPC that someone entered.< / p > < / li >
2022-11-21 23:18:31 +00:00
< li > < p > We will also tweak our default < code class = "docutils literal notranslate" > < span class = "pre" > Character< / span > < / code > typeclass a little.< / p > < / li >
< / ul >
< div class = "highlight-python notranslate" > < div class = "highlight" > < pre > < span > < / span > < span class = "c1" > # in mygame/typeclasses/npcs.py (for example)< / span >
< span class = "kn" > from< / span > < span class = "nn" > typeclasses.characters< / span > < span class = "kn" > import< / span > < span class = "n" > Character< / span >
< span class = "k" > class< / span > < span class = "nc" > NPC< / span > < span class = "p" > (< / span > < span class = "n" > Character< / span > < span class = "p" > ):< / span >
2023-01-06 17:19:13 +00:00
< span class = "w" > < / span > < span class = "sd" > " " " < / span >
2022-11-21 23:18:31 +00:00
< span class = "sd" > A NPC typeclass which extends the character class.< / span >
< span class = "sd" > " " " < / span >
< span class = "k" > def< / span > < span class = "nf" > at_char_entered< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ,< / span > < span class = "n" > character< / span > < span class = "p" > ):< / span >
2023-01-06 17:19:13 +00:00
< span class = "w" > < / span > < span class = "sd" > " " " < / span >
2022-11-21 23:18:31 +00:00
< span class = "sd" > A simple is_aggressive check.< / span >
< span class = "sd" > Can be expanded upon later.< / span >
< span class = "sd" > " " " < / span >
< span class = "k" > if< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > db< / span > < span class = "o" > .< / span > < span class = "n" > is_aggressive< / span > < span class = "p" > :< / span >
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > execute_cmd< / span > < span class = "p" > (< / span > < span class = "sa" > f< / span > < span class = "s2" > " say Graaah! Die, < / span > < span class = "si" > {< / span > < span class = "n" > character< / span > < span class = "si" > }< / span > < span class = "s2" > !" < / span > < span class = "p" > )< / span >
< span class = "k" > else< / span > < span class = "p" > :< / span >
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > execute_cmd< / span > < span class = "p" > (< / span > < span class = "sa" > f< / span > < span class = "s2" > " say Greetings, < / span > < span class = "si" > {< / span > < span class = "n" > character< / span > < span class = "si" > }< / span > < span class = "s2" > !" < / span > < span class = "p" > )< / span >
< / pre > < / div >
< / div >
< p > Here we make a simple method on the < code class = "docutils literal notranslate" > < span class = "pre" > NPC< / span > < / code > ˙. We expect it to be called when a (player-)character enters the room. We don’ t actually set the < code class = "docutils literal notranslate" > < span class = "pre" > is_aggressive< / span > < / code > < a class = "reference internal" href = "../Components/Attributes.html" > < span class = "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 < code class = "docutils literal notranslate" > < span class = "pre" > Room< / span > < / code > , its < a class = "reference internal" href = "../api/evennia.objects.objects.html#evennia.objects.objects.DefaultObject.at_object_receive" title = "evennia.objects.objects.DefaultObject.at_object_receive" > < span class = "xref myst py py-meth" > at_object_receive< / span > < / a > hook will be called. So we should override it.< / p >
< div class = "highlight-python notranslate" > < div class = "highlight" > < pre > < span > < / span > < span class = "c1" > # in mygame/typeclasses/rooms.py< / span >
< span class = "kn" > from< / span > < span class = "nn" > evennia< / span > < span class = "kn" > import< / span > < span class = "n" > utils< / span >
< span class = "c1" > # ... < / span >
< span class = "k" > class< / span > < span class = "nc" > Room< / span > < span class = "p" > (< / span > < span class = "n" > ObjectParent< / span > < span class = "p" > ,< / span > < span class = "n" > DefaultRoom< / span > < span class = "p" > ):< / span >
< span class = "c1" > # ... < / span >
< span class = "k" > def< / span > < span class = "nf" > at_object_receive< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ,< / span > < span class = "n" > arriving_obj< / span > < span class = "p" > ,< / span > < span class = "n" > source_location< / span > < span class = "p" > ):< / span >
< span class = "k" > if< / span > < span class = "n" > arriving_obj< / span > < span class = "o" > .< / span > < span class = "n" > account< / span > < span class = "p" > :< / span >
< span class = "c1" > # this has an active acccount - a player character< / span >
< span class = "k" > for< / span > < span class = "n" > item< / span > < span class = "ow" > in< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > contents< / span > < span class = "p" > :< / span >
< span class = "c1" > # get all npcs in the room and inform them< / span >
< span class = "k" > if< / span > < span class = "n" > utils< / span > < span class = "o" > .< / span > < span class = "n" > inherits_from< / span > < span class = "p" > (< / span > < span class = "n" > item< / span > < span class = "p" > ,< / span > < span class = "s2" > " typeclasses.npcs.NPC" < / span > < span class = "p" > ):< / span >
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > at_char_entered< / span > < span class = "p" > (< / span > < span class = "n" > arriving_obj< / span > < span class = "p" > )< / span >
< / pre > < / div >
< / div >
< aside class = "sidebar" >
< p class = "sidebar-title" > Universal Object methods< / p >
< p > Remember that Rooms are < code class = "docutils literal notranslate" > < span class = "pre" > Objects< / span > < / code > . So the same < code class = "docutils literal notranslate" > < span class = "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 < code class = "docutils literal notranslate" > < span class = "pre" > .account< / span > < / code > attached to it. We use that to know that the thing arriving is a Character. We then use Evennia’ s < a class = "reference internal" href = "../api/evennia.utils.utils.html#evennia.utils.utils.inherits_from" title = "evennia.utils.utils.inherits_from" > < span class = "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 < code class = "docutils literal notranslate" > < span class = "pre" > at_char_entered< / span > < / code > method.< / p >
< p > Make sure to < code class = "docutils literal notranslate" > < span class = "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 >
< div class = "highlight-none notranslate" > < div class = "highlight" > < pre > < span > < / span > > create/drop Orc:typeclasses.npcs.NPC
> north
> south
Orc says, Greetings, Anna!
< / pre > < / div >
< / div >
< p > Now let’ s turn the orc aggressive.< / p >
< div class = "highlight-none notranslate" > < div class = "highlight" > < pre > < span > < / span > > set orc/is_aggressive = True
> north
> south
Orc says, Graah! Die, Anna!
< / pre > < / div >
< / div >
< p > That’ s one easily aggravated Orc!< / p >
< / section >
< / div >
< / div >
< / div >
< / div >
< div class = "related" role = "navigation" aria-label = "related navigation" >
< h3 > Navigation< / h3 >
< ul >
< li class = "right" style = "margin-right: 10px" >
< a href = "../genindex.html" title = "General Index"
>index< / a > < / li >
< li class = "right" >
< a href = "../py-modindex.html" title = "Python Module Index"
>modules< / a > |< / li >
< li class = "right" >
< a href = "Tutorial-NPC-Merchants.html" title = "NPC merchants"
>next< / a > |< / li >
< li class = "right" >
< a href = "Tutorial-NPC-Listening.html" title = "NPCs that listen to what is said"
>previous< / a > |< / li >
2023-10-19 20:22:27 +00:00
< li class = "nav-item nav-item-0" > < a href = "../index.html" > Evennia 2.x< / a > » < / li >
2023-12-20 18:20:52 +01:00
< li class = "nav-item nav-item-1" > < a href = "Howtos-Overview.html" > Tutorials and Howto’ s< / a > » < / li >
2022-11-21 23:18:31 +00:00
< li class = "nav-item nav-item-this" > < a href = "" > NPCs reacting to your presence< / a > < / li >
< / ul >
< / div >
< div class = "footer" role = "contentinfo" >
2023-10-19 20:22:27 +00:00
© Copyright 2023, The Evennia developer community.
2022-11-21 23:18:31 +00:00
Created using < a href = "https://www.sphinx-doc.org/" > Sphinx< / a > 3.2.1.
< / div >
< / body >
< / html >