Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
NPC NPC Persister  

NPC Persister

When a 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.

Category: NPC
By : Marcus Llewellyn
Created: 2014-01-17 Edited: 2014-01-17
Worlds: OpenSim

the Zip file

Download all files for NPC Persister
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. NPC Persister.lsl
1 // Implementing NPC Persistence
2
3 // The 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.
4 // 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.
5 // NPC Persistance Example created by Marcus Llewellyn.
6 // This script is in the Public Domain.
7
8 key npc = NULL_KEY;
9 string firstname = "Dwight";
10 string lastname = "Smith";
11 integer dead = FALSE;
12
13 default
14 {
15 state_entry() {
16 // Setup and rez the NPC.
17 key temp = (key)llGetObjectDesc();
18 if(llKey2Name(temp) != "") {
19 // An NPC matching the UUID stored in the object description
20 // already exists, so just retrieve the UUID.
21 npc = temp;
22 } else if(dead == FALSE) {
23 // Create a new instance of the NPC, record the UUID in the
24 // object's description, and set starting rotation. NPC
25 // rotation and location are inherited from the controlling
26 // object with an offset.
27 npc = osNpcCreate(firstname, lastname, llGetPos() + <1.0,0.0,0.0>, llGetOwner());
29 osNpcSetRot(npc, llGetRot() * (llEuler2Rot(<0, 0, 90> * DEG_TO_RAD)));
30 }
31 // Have the NPC say a greeting, and set up persistance timer and
32 // listen for commands.
33 osNpcSay(npc, firstname + " " + lastname + ", at your service.");
35 llListen(0, "", NULL_KEY, "");
36
37 }
38
39 timer() {
40 // Our NPC UUID stored in the object description should match the
41 // UUID of our existing NPC. If it does not, we presume an untimely
42 // demise, and initiate resurrection by simply reseting our script.
43 key temp = (key)llGetObjectDesc();
44 if(llKey2Name(temp) == "" && dead == FALSE) {
46 }
47 }
48
49 listen(integer channel, string name, key id, string msg) {
50 if(llToLower(msg) == "kill") {
51 // Kill the NPC, set a flag so it stays dead, and misquote
52 // John Donne.
53 osNpcSay(npc, "Send not to know for whom the bell tolls, it tolls for me!");
54 osNpcRemove(npc);
55 dead = TRUE;
56 } else if(llToLower(msg) == "start" && dead == TRUE) {
57 // Create a new instance of our NPC, and set flag for
58 // persistance checks.
59 npc = osNpcCreate(firstname, lastname, llGetPos() + <1.0,0.0,0.0>, llGetOwner());
61 osNpcSetRot(npc, llGetRot() * (llEuler2Rot(<0, 0, 90> * DEG_TO_RAD)));
62 osNpcSay(npc, firstname + " " + lastname + ", at your service.");
63 dead = FALSE;
64 } else if(llToLower(msg) == "start" && dead == FALSE) {
65 // Don't do anything significant if the NPC is still incarnate.
66 osNpcSay(npc, "I'm already alive, boss.");
67 }
68 }
69 }

Back to the Best Free Tools in Second Life and OpenSim.