Channels ▼

Open Source

Second Life: A Programmer's Perspective

Source Code Accompanies This Article. Download It Now.

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).

Command Description
/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.

Table 2: Chat Catcher commands.

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 create—say, 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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.