Chat channel

From Second Life Wiki
Jump to: navigation, search

Chat Channel

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. chat channels range from -2147483648 to 2147483647 with DEBUG_CHANNEL being on 2147483647.

Chat can be heard within a sphere centered on the sender:

function purpose
llWhisper 10m
llSay 20m
llShout 100m
llRegionSay Entire Region


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

// 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);
    }
}

Private Channel

Would you like your channel to be custom or private for each owner or each object? Well then you can use these functions to provide a way to have a so called "private" channel per owner or object.

This function will provide each object with a separate unique integer.

integer Pchannel(string salt){
    string setint = (string)((integer)("0x"+llGetSubString((string)llGetKey(),-8,-1)) * llStringLength(salt) * llCeil(llLog(llStringLength(salt))));
    return (integer)llGetSubString(setint, 0, 7);
}

This function will provide each owner with a separate unique integer.

integer Pchannel(string salt){
    string setint = (string)((integer)("0x"+llGetSubString((string)llGetOwner(),-8,-1)) * llStringLength(salt) * llCeil(llLog(llStringLength(salt))));
    return (integer)llGetSubString(setint, 0, 7);
}