Connecting SL to Real Life
This script demonstrates both a typical finite state machine and some of Second Life's asynchronous communication capability. Second Life supports XML-RPC and HttpRequests. You can also write handler methods capable of sending/receiving e-mail message traffic. This exercise demonstrates the e-mail interface. You create an object (a "Chat Catcher") that exists in the virtual world, sitting apparently dormant, but reporting the ongoing chat to an e-mail address of your choice. To preclude the possibility of an e-mail flood, you set a short timer to shut off the object's listening capability.
The Chat Catcher script creates a listening channel (channel 2), and e-mails anything uttered to the e-mail address. Everything is contained within the default block (but again, it needn't be). The first dozen or so lines declare and instantiate useful global variables. The g_maxtimeout is set to three minutes, but you may want to experiment with this. The general chat channel (g_listen_channel) is 0. A private channel (g_command_channel) is set aside for the object to listen to commands from its owner.
On line 17, the script is reset using llResetScript. This is a good idea, especially if the script is attached to an object intended for sale or transfer. On transfer to a new owner, any listens set to the owner are reset, and all pending events are cleared when the object is rezzed by its new owner. After an llResetScript invocation, control is transferred to the default block, and its state_entry is triggered. When writing and debugging the script, you can achieve the same effect as specifying "reset" in the script editor.
Lines 22 through 31 are the initializers for the default script block. The script transfers ownership of the object to which the script is attached to the new owner. Next, lines 25-26, the object posts a listen on channels 0 and 2. Then, the object issues two private messages to the object owner, and sets the operational maximum time for the script to operate (lines 28-30). In the listen handler, whenever a message is received on channel 2, the object parses it (lines 35-52). The object is looking to receive one of the messages in Table 2 (typed into the chat window by the object's creator).
|/2 hear! emailname@email_address||Forwards any conversation in the object's proximity to the e-mail address specified as the second argument:|
|/2 !hear||Cancels any listens on the public channel (channel 0).|
|/2 IM!||Private messages the general (channel 0) conversation to the objects owner, even if the owner is no longer in the proximity of the conversation (teleported to another place, for example).|
|/2 !IM||Cancels the forwarding of public messages as private messages to the object owner.|
On Line 36, the listener uses llParseString2List to break the input message into a list of strings. The command itself is the zero-th element of the list; this is pulled from the list on line 38. Note that the message channel designator (/2) is not a part of the message payload, and messages to the command channel must be prefaced with this command channel qualifier. Any messages, from the object owner or anyone else in proximity, are heard on channel 0, and no channel designator is needed for public chat messages.
If the "hear!" message is incomplete, this is checked in lines 44-46, and a private message is sent back to the object owner. If the message has an e-mail address, the e-mail address is teased from the message body (which by now has been parsed into a list). Line 38 splits the message body into a list with whitespace as the separator. This code doesn't check the e-mail address for valid syntax, but this could be easily done by turning the string into a list with the @ sign as the separator, and looking at the list length.
Lines 53-66 complete command channel processing to turn private messaging on/off. On line 68, an e-mail message is sent to the e-mail address provided.
Try this out on any object you createsay, a beach ball that encourages a crowd to pass it around, and you'll have one in your inventory courtesy of Linden Lab. Some privately owned land in Second Life might not permit you to instantiate the object.
Second Life is full of opportunities to create useful and beautiful artifacts with an unlimited range of behaviors. Try taking developer classes in-world, advertising to meet other developers, and joining one of the many developers' groups in-world. In no time, you'll find yourself amongst the creative elite of Second Life developers.