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