<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://opensimulator.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://opensimulator.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=7Nqe93KzcT</id>
		<title>OpenSimulator - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://opensimulator.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=7Nqe93KzcT"/>
		<link rel="alternate" type="text/html" href="http://opensimulator.org/wiki/Special:Contributions/7Nqe93KzcT"/>
		<updated>2026-04-10T15:38:51Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.19.9</generator>

	<entry>
		<id>http://opensimulator.org/wiki/OSSL_Script_Library/Persist_NPC</id>
		<title>OSSL Script Library/Persist NPC</title>
		<link rel="alternate" type="text/html" href="http://opensimulator.org/wiki/OSSL_Script_Library/Persist_NPC"/>
				<updated>2011-11-03T04:31:52Z</updated>
		
		<summary type="html">&lt;p&gt;7Nqe93KzcT: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;When a [http://www.sproutseo.com SEO service] simulator is restarted, any NPCs on that sim's regions will not automatically reappear. Much like dynamic textures, NPCs are a transient and non-persistant asset that is not stored in any database. So any scripts that controls an NPC will need to have a mechanism in place to detect wether an NPC is currently instantiated, and recreate it if it is not. &lt;br /&gt;
&lt;br /&gt;
== Implementing NPC Persistence&amp;lt;br&amp;gt;  ==&lt;br /&gt;
&lt;br /&gt;
The [http://lslwiki.net/lslwiki/wakka.php?wakka=llKey2Name llKey2Name] function placed in a timer event is the tool I use to check whether an NPC is still present in-world. If this function returns an empty string, it can be assumed that the NPC is no longer rezzed, and must be recreated. &lt;br /&gt;
&lt;br /&gt;
The following script is an example of the mechanism I make use of to accomplish this task. It has been kept to the bare minimum necessary to demonstrate the concept, and to manage the NPC it creates. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lsl&amp;quot;&amp;gt;// NPC Persistance Example created by Marcus Llewellyn.&lt;br /&gt;
// This script is in the Public Domain.&lt;br /&gt;
&lt;br /&gt;
key npc = NULL_KEY;&lt;br /&gt;
string firstname = &amp;quot;Dwight&amp;quot;;&lt;br /&gt;
string lastname = &amp;quot;Smith&amp;quot;;&lt;br /&gt;
integer dead = FALSE;&lt;br /&gt;
&lt;br /&gt;
default&lt;br /&gt;
{&lt;br /&gt;
    state_entry() {&lt;br /&gt;
        // Setup and rez the NPC.&lt;br /&gt;
        key temp = (key)llGetObjectDesc();&lt;br /&gt;
        if (llKey2Name(temp) != &amp;quot;&amp;quot;) {&lt;br /&gt;
            // An NPC matching the UUID stored in the object description&lt;br /&gt;
            // already exists, so just retrieve the UUID.&lt;br /&gt;
            npc = temp;&lt;br /&gt;
        } else if (dead == FALSE) {&lt;br /&gt;
            // Create a new instance of the NPC, record the UUID in the&lt;br /&gt;
            // object's description, and set starting rotation. NPC&lt;br /&gt;
            // rotation and location are inherited from the controlling&lt;br /&gt;
            // object with an offset.&lt;br /&gt;
            npc = osNpcCreate(firstname, lastname, llGetPos() + &amp;lt;1.0,0.0,0.0&amp;gt;, llGetOwner());&lt;br /&gt;
            llSetObjectDesc((string)npc);&lt;br /&gt;
            osNpcSetRot(npc, llGetRot() * (llEuler2Rot(&amp;lt;0, 0, 90&amp;gt; * DEG_TO_RAD)));&lt;br /&gt;
        }&lt;br /&gt;
        // Have the NPC say a greeting, and set up persistance timer and&lt;br /&gt;
        // listen for commands.&lt;br /&gt;
        osNpcSay(npc, firstname + &amp;quot; &amp;quot; + lastname + &amp;quot;, at your service.&amp;quot;);&lt;br /&gt;
        llSetTimerEvent(10);&lt;br /&gt;
        llListen(0, &amp;quot;&amp;quot;, NULL_KEY, &amp;quot;&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    timer() {&lt;br /&gt;
        // Our NPC UUID stored in the object description should match the&lt;br /&gt;
        // UUID of our existing NPC. If it does not, we presume an untimely&lt;br /&gt;
        // demise, and initiate resurrection by simply reseting our script.&lt;br /&gt;
        key temp = (key)llGetObjectDesc();&lt;br /&gt;
        if (llKey2Name(temp) == &amp;quot;&amp;quot; &amp;amp;&amp;amp; dead == FALSE) {&lt;br /&gt;
            llResetScript();&lt;br /&gt;
        }          &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    listen(integer channel, string name, key id, string msg) {&lt;br /&gt;
        if (llToLower(msg) == &amp;quot;kill&amp;quot;) {&lt;br /&gt;
            // Kill the NPC, set a flag so it stays dead, and misquote&lt;br /&gt;
            // John Donne.&lt;br /&gt;
            osNpcSay(npc, &amp;quot;Send not to know for whom the bell tolls, it tolls for me!&amp;quot;);&lt;br /&gt;
            osNpcRemove(npc);&lt;br /&gt;
            dead = TRUE;&lt;br /&gt;
        } else if (llToLower(msg) == &amp;quot;start&amp;quot; &amp;amp;&amp;amp; dead == TRUE) {&lt;br /&gt;
            // Create a new instance of our NPC, and set flag for&lt;br /&gt;
            // persistance checks.&lt;br /&gt;
            npc = osNpcCreate(firstname, lastname, llGetPos() + &amp;lt;1.0,0.0,0.0&amp;gt;, llGetOwner());&lt;br /&gt;
            llSetObjectDesc((string)npc);&lt;br /&gt;
            osNpcSetRot(npc, llGetRot() * (llEuler2Rot(&amp;lt;0, 0, 90&amp;gt; * DEG_TO_RAD)));&lt;br /&gt;
            osNpcSay(npc, firstname + &amp;quot; &amp;quot; + lastname + &amp;quot;, at your service.&amp;quot;);&lt;br /&gt;
            dead = FALSE;&lt;br /&gt;
        } else if (llToLower(msg) == &amp;quot;start&amp;quot; &amp;amp;&amp;amp; dead == FALSE) {&lt;br /&gt;
            // Don't do anything significant if the NPC is still incarnate.&lt;br /&gt;
            osNpcSay(npc, &amp;quot;I'm already alive, boss.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>7Nqe93KzcT</name></author>	</entry>

	</feed>