Difference between revisions of "Chat channel"

From Second Life Wiki
Jump to navigation Jump to search
(Added Radio Tuner script)
Line 4: Line 4:


To add more resilience to a protocol, in can be a good idea to match some other criteria before trying to parse a message. Don't expect that others will avoid using your channel simply because you use it.  For instance, use a unique starting header on your message and check to see if it exists. This way multiple devices can use the same channels.
To add more resilience to a protocol, in can be a good idea to match some other criteria before trying to parse a message. Don't expect that others will avoid using your channel simply because you use it.  For instance, use a unique starting header on your message and check to see if it exists. This way multiple devices can use the same channels.
== Listening in on a channel ==
<lsl>// Radio Tuner
// Lets you listen in on any channel except 0
integer listen_handle = 0;
integer channel = 0;
TuneTo(integer chan)
{
    channel = chan;
   
    if (listen_handle)
        llListenRemove(listen_handle);
   
    if (!chan)
    {
        listen_handle = 0;
        llOwnerSay("Radio turned off.");
        return;
    }
   
    listen_handle = llListen(chan, "", NULL_KEY, "");
    llOwnerSay("Listening to channel " + (string)chan);
}
default
{
    attach(key attached)
    {
        if (attached != NULL_KEY)
            llResetScript();
    }
   
    state_entry()
    {
        llListen(96, "", llGetOwner(), "");
        llOwnerSay("Type \"/96 tune N\" to tune in to channel N, or \"/96 tune 0\" to turn radio off.");
    }
   
    listen(integer ch, string name, key id, string msg)
    {
        if ((ch == 96) && llGetSubString(msg, 0, 4) == "tune ")
            TuneTo((integer)llGetSubString(msg, 5, -1));
        else if (ch == channel)
            llOwnerSay("[" + (string)ch + " " + name + "]: " + msg);
    }
}
</lsl>

Revision as of 10:03, 25 December 2007

In Second Life, resident chat can be heard by all nearby because it all goes over an open channel, which has the number 0.

Often, in scripts, different objects need to exchange information or coordinate their behavior. One way to do that is to have them chat on a channel known to all of them. All the scripts that are listening on a channel will hear whatever is said on that channel, up to various distance restrictions. See llListen, llSay, and llRegionSay for more details.

To add more resilience to a protocol, in can be a good idea to match some other criteria before trying to parse a message. Don't expect that others will avoid using your channel simply because you use it. For instance, use a unique starting header on your message and check to see if it exists. This way multiple devices can use the same channels.

Listening in on a channel

<lsl>// Radio Tuner // Lets you listen in on any channel except 0

integer listen_handle = 0; integer channel = 0;

TuneTo(integer chan) {

   channel = chan;
   
   if (listen_handle)
       llListenRemove(listen_handle);
   
   if (!chan)
   {
       listen_handle = 0;
       llOwnerSay("Radio turned off.");
       return;
   }
   
   listen_handle = llListen(chan, "", NULL_KEY, "");
   llOwnerSay("Listening to channel " + (string)chan);

}

default {

   attach(key attached)
   {
       if (attached != NULL_KEY)
           llResetScript();
   }
   
   state_entry()
   {
       llListen(96, "", llGetOwner(), "");
       llOwnerSay("Type \"/96 tune N\" to tune in to channel N, or \"/96 tune 0\" to turn radio off.");
   }
   
   listen(integer ch, string name, key id, string msg)
   {
       if ((ch == 96) && llGetSubString(msg, 0, 4) == "tune ")
           TuneTo((integer)llGetSubString(msg, 5, -1));
       else if (ch == channel)
           llOwnerSay("[" + (string)ch + " " + name + "]: " + msg);
   }

} </lsl>