Scripthackery: A Kind of Magic

So, last night, Danielle and I were hanging out with Skye Galileo and Alexander Lapointe at Danielle’s new store, Don’t Panic (conveniently located near the Cutlass Club). Alex had with him an object he’d created at a Building Shelter competition…a doll that bears an amazing resemblance to Travis Lambert, one of the proprietors of The Shelter. He’d added a simple script to make the doll say things…so simple, in fact, that, in order to change what the doll said, he had to edit the script. Being the hacker-type that I am, I suggested that what he needed was a simple chat relay, allowing him to “put words in the doll’s mouth” on a private channel. And so I sat down and whacked out this little script:

// Travis Chat Relay Script
// Erbo Evans - 8/14/2006
////////////////////////////

default
{
  state_entry()
  {
    llListen(222,"",llGetOwner(),"");
    llOwnerSay("Type /222 message to make Travis say stuff.");
  }

  on_rez(integer param)
  {
    llResetScript();
  }

  listen(integer channel, string name, key id, string message)
  {
    if (channel!=222)
      return;
    llSay(0,message);
  }
}

For the benefit of you in the audience who are not scripters yourselves, here’s what this does. The state_entry() event handler, which is run when the script starts, sets up a listener on chat channel 222 for messages from the object’s owner only. It then tells the owner how to make the doll say stuff. The listen() event handler just takes all messages received on channel 222 and makes the doll say them on channel 0 (the public channel). (The on_rez() handler just resets the script to ensure that the listener gets installed. I admit that I didn’t add this until later, and it isn’t always the right thing to do in every script, but ths one is so simple it hardly matters otherwise.)

Alex was delighted with this little toy, and wanted a slightly more advanced version that would allow the doll to say, for instance, the Shelter’s standard “scold” message with a shortcut similar to the standard gesture that gets used there. So I enhanced the script to look more like this:

// Travis Chat Relay Script with macros
// Erbo Evans - 8/14/2006
////////////////////////////////////////

list macros = [ "/scold",
"Everyone is welcome @ The Shelter but guns, nudity, and foul language are not.  Repeated warnings will result in banning!" ];

default
{
  state_entry()
  {
    llListen(222,"",llGetOwner(),"");
    llOwnerSay("Type /222 message to make Travis say stuff.");
  }

  on_rez(integer param)
  {
    llResetScript();
  }

  listen(integer channel, string name, key id, string message)
  {
    if (channel!=222)
      return;
    integer len = llGetListLength(macros);
    integer i;
    for (i=0; i<len; i+=2)
    {
      string s = llList2String(macros,i);
      if (s==message)
      {
        llSay(0,llList2String(macros,i + 1));
        return;
      }
    }
    llSay(0,message);
  }
}

Now, when the script gets an incoming chat message, it runs through the “macros” list, taking the elements in pairs. If the incoming message matches the first element of a pair, it says the second element of the pair instead. This makes it easy to add new “macros” by just adding more pairs of elements to the end of the list.

Now, my point, because I did have one, was that, to everyone else there, this looked like magic. Neither Skye nor Danielle knows anything about scripting, and Alex not much more. Yet, to me, the code was so blindingly obvious, I could whack it out with little more than looking up a couple of API calls on the Scripting Wiki–which is no different from what I have to do in Java, say. I guess it helps if you do this kind of stuff for a living….

I do need to continue cultivating my LSL skills; there’s a need for good programmers out there, whether “out there” is in RL or SL. And I know I’m not yet the equal of some of the scripters out there. We shall see how far this takes me.

Advertisements

7 Comments

Filed under Scripthackery

7 responses to “Scripthackery: A Kind of Magic

  1. Well it is not like I am not learning. I can edit script, I am still learning. 🙂

  2. Freely acknowledged. Perhaps this posting and future ones I do on scripting will help you and others learn as well. That would be a noteworthy goal.

  3. Haha, that was a great night! It was wonderful meeting both you and Dani.

  4. Ahhh, so you’re whom we have to ‘thank’ for the Talking Travis…and his off-color commentary. 😉

  5. Reina, I just provide the tools…it’s up to other people whether to use them for good or ill. Right, Alex? 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s