Difference between revisions of "Listen"

From Second Life Wiki
Jump to: navigation, search
m (added another example with auto-closing listeners)
m ('avis' isn't a slang we currently use in the documentation. Using it doesn't gain us anything and could cause confusion.)
 
(5 intermediate revisions by 5 users not shown)
Line 9: Line 9:
 
|constants={{LSL Constants/Chat}}
 
|constants={{LSL Constants/Chat}}
 
|spec
 
|spec
|caveats=*On [[state]] change or [[llResetScript|script reset]] all listens are [[llListenRemove|closed]] automaticaly.
+
|caveats=*On [[state]] change or [[llResetScript|script reset]] all listens are [[llListenRemove|closed]] automatically.
 
*When an object changes owner any listen registered with [[llGetOwner]] will not automatically update itself until the script is reset. The scripter can catch this scenario per the example below.
 
*When an object changes owner any listen registered with [[llGetOwner]] will not automatically update itself until the script is reset. The scripter can catch this scenario per the example below.
 
*If a message satisfies the filters of multiple [[llListen]]s registered by the script, only one event will be raised.
 
*If a message satisfies the filters of multiple [[llListen]]s registered by the script, only one event will be raised.
 
*A prim cannot hear/listen to chat it generates.
 
*A prim cannot hear/listen to chat it generates.
 
*The location of the listen is not at the listening prim's location but at the root prim's location. This is to deter people using child prims for spying over parcel boundaries. {{LSLGC|Chat|Chat generating functions}} on the other hand generate chat at the calling prim's location (and not at the root prim's location).
 
*The location of the listen is not at the listening prim's location but at the root prim's location. This is to deter people using child prims for spying over parcel boundaries. {{LSLGC|Chat|Chat generating functions}} on the other hand generate chat at the calling prim's location (and not at the root prim's location).
 +
*The above is not true for chat generated by avatars sitting on the same object as the listening prim.
 
|examples=
 
|examples=
{{Warning|Please make sure that you close open listeners where possible. You'll make the Second Life experience so much better when paying attention to details here.}}
+
{{LSL Tip|Please make sure that you close open listeners where possible. You'll make the Second Life experience so much better when paying attention to details here.}}
 
Trivial example to listen to any chat from the object owner and respond once. To reduce lag and avoid spamming surrounding users, it is vastly preferable to listen on channels other than 0 and to trigger the listen event by chatting on an alternative channel such as '/5 hello'.
 
Trivial example to listen to any chat from the object owner and respond once. To reduce lag and avoid spamming surrounding users, it is vastly preferable to listen on channels other than 0 and to trigger the listen event by chatting on an alternative channel such as '/5 hello'.
<lsl>// says beep to owner the first time owner says something in main chat;
+
<source lang="lsl2">
 +
// says beep to owner the first time owner says something in main chat;
 
integer listen_handle;
 
integer listen_handle;
  
Line 27: Line 29:
 
         listen_handle = llListen(0, "", llGetOwner(), "");
 
         listen_handle = llListen(0, "", llGetOwner(), "");
 
     }
 
     }
 +
 
     listen( integer channel, string name, key id, string message )
 
     listen( integer channel, string name, key id, string message )
 
     {
 
     {
Line 33: Line 36:
 
         llListenRemove(listen_handle);
 
         llListenRemove(listen_handle);
 
     }
 
     }
    on_rez(integer param)
+
 
    {  //Triggered when the object is rezed, like after the object had been sold from a vendor
+
        llResetScript();//By resetting the script on rez it forces the listen to re-register.
+
    }
+
 
     changed(integer mask)
 
     changed(integer mask)
 
     {  //Triggered when the object containing this script changes owner.
 
     {  //Triggered when the object containing this script changes owner.
 
         if(mask & CHANGED_OWNER)
 
         if(mask & CHANGED_OWNER)
 
         {
 
         {
             llResetScript();
+
             llResetScript();   // This will ensure the script listens to the new owner, and doesn't continue listening to the creator.
 
         }
 
         }
 
     }
 
     }
}</lsl>
+
}</source>
<lsl>
+
string mainMenuDialog = "\nWhich settings would you like to access?\n"
+
    + "Click \"Close\" to close the menu.\n\n"
+
    + "You are here:\nMainmenu";
+
list mainMenuButtons = ["sub 01", "sub 02", "Close"];
+
  
string subMenu_01_Dialog = "\nClick \"Close\" to close the menu.\n"
 
    + "Click \"-Main-\" to return to the main menu.\n\n"
 
    + "You are here:\nMainmenu > sub 01";
 
list subMenu_01_Buttons = ["action 01a", "action 01b", "Close", "-Main-"];
 
 
string subMenu_02_Dialog = "\nClick \"Close\" to close the menu.\n"
 
    + "Click \"-Main-\" to return to the main menu.\n\n"
 
    + "You are here:\nMainmenu > sub 02";
 
list subMenu_02_Buttons = ["action 02a", "action 02b", "Close", "-Main-"];
 
 
integer dialogChannel;
 
integer dialogHandle;
 
 
open_menu(key inputKey, string inputString, list inputList)
 
{
 
    dialogChannel = (integer)llFrand(DEBUG_CHANNEL)*-1;
 
    dialogHandle = llListen(dialogChannel, "", inputKey, "");
 
    //llListenControl(dialogHandle,TRUE);
 
    llDialog(inputKey, inputString, inputList, dialogChannel);
 
    llSetTimerEvent(30.0);
 
}
 
 
close_menu()
 
{
 
    llSetTimerEvent((float)FALSE);
 
    //llListenControl(dialogHandle,FALSE);
 
    llListenRemove(dialogHandle);
 
    dialogHandle = FALSE;
 
    dialogChannel = (integer)llFrand(DEBUG_CHANNEL)*-1;
 
}
 
 
default
 
{
 
    on_rez(integer start_param)
 
    {
 
        llResetScript();
 
    }
 
 
    touch_start(integer total_number)
 
    {
 
        key id = llDetectedKey(0);
 
 
        open_menu(id, mainMenuDialog, mainMenuButtons);
 
    }
 
 
    listen(integer channel, string name, key id, string message)
 
    {
 
        if(channel != dialogChannel)
 
            return;
 
 
        close_menu();
 
 
        if(message == "-Main-")
 
            open_menu(id, mainMenuDialog, mainMenuButtons);
 
 
        else if(message == "sub 01")
 
            open_menu(id, subMenu_01_Dialog, subMenu_01_Buttons);
 
 
        else if(message == "sub 02")
 
            open_menu(id, subMenu_02_Dialog, subMenu_02_Buttons);
 
 
        else if (message == "action 01a")
 
        {
 
            //do something
 
            open_menu(id, subMenu_01_Dialog, subMenu_01_Buttons);
 
        }
 
        else if (message == "action 01b")
 
        {
 
            //do something else
 
 
            //maybe not re-open the menu for this option?
 
            //open_menu(id, subMenu_01_Dialog, subMenu_01_Buttons);
 
        }
 
        else if (message == "action 02a")
 
        {
 
            //do something
 
            open_menu(id, subMenu_02_Dialog, subMenu_02_Buttons);
 
        }
 
        else if (message == "action 02b")
 
        {
 
            //do something else
 
            open_menu(id, subMenu_02_Dialog, subMenu_02_Buttons);
 
        }
 
    }
 
 
    timer()
 
    {
 
        close_menu();
 
    }
 
}
 
</lsl>
 
 
|helpers
 
|helpers
 
|also_header
 
|also_header

Latest revision as of 18:49, 15 April 2015

Description

Event: listen( integer channel, string name, key id, string message ){ ; }

Triggered by chat, use llListen to enable and filter

• integer channel channel that the message appeared on.
• string name prim name or avatar Legacy Name
• key id group, avatar or prim UUID
• string message text spoken
Channel Constant Description
DEBUG_CHANNEL 0x7FFFFFFF Chat channel reserved for script debugging and error messages, broadcasts to all nearby users.
PUBLIC_CHANNEL 0x0 Chat channel that broadcasts to all nearby users. This channel is sometimes referred to as: open chat, local chat and public chat.

Caveats

  • On state change or script reset all listens are closed automatically.
  • When an object changes owner any listen registered with llGetOwner will not automatically update itself until the script is reset. The scripter can catch this scenario per the example below.
  • If a message satisfies the filters of multiple llListens registered by the script, only one event will be raised.
  • A prim cannot hear/listen to chat it generates.
  • The location of the listen is not at the listening prim's location but at the root prim's location. This is to deter people using child prims for spying over parcel boundaries. Chat generating functions on the other hand generate chat at the calling prim's location (and not at the root prim's location).
  • The above is not true for chat generated by avatars sitting on the same object as the listening prim.
All Issues ~ Search JIRA for related Bugs

Examples

KBcaution.png Important: Please make sure that you close open listeners where possible. You'll make the Second Life experience so much better when paying attention to details here.

Trivial example to listen to any chat from the object owner and respond once. To reduce lag and avoid spamming surrounding users, it is vastly preferable to listen on channels other than 0 and to trigger the listen event by chatting on an alternative channel such as '/5 hello'.

// says beep to owner the first time owner says something in main chat;
integer listen_handle;
 
default
{
    state_entry()
    {   //Registers the listen to the owner of the object at the moment of the call. This does not automatically update when the owner changes.
        // Change 0 to another positive number to listen for '/5 hello' style of chat.
        listen_handle = llListen(0, "", llGetOwner(), "");
    }
 
    listen( integer channel, string name, key id, string message )
    {
        llOwnerSay("beep");
        // Stop listening until script is reset
        llListenRemove(listen_handle);
    }
 
    changed(integer mask)
    {   //Triggered when the object containing this script changes owner.
        if(mask & CHANGED_OWNER)
        {
            llResetScript();   // This will ensure the script listens to the new owner, and doesn't continue listening to the creator.
        }
    }
}

See Also

Functions

•  llListen
•  llListenControl
•  llListenRemove
•  llDialog
•  llOwnerSay Sends chat to the owner only to avoid spamming the PUBLIC_CHANNEL
•  llWhisper Sends chat limited to 10 meters
•  llSay Sends chat limited to 20 meters
•  llShout Sends chat limited to 100 meters
•  llRegionSay Sends chat limited to region

Deep Notes

Issues

All Issues

~ Search JIRA for related Issues
   llTargetSay() - region-wide direct communication

Signature

event void listen( integer channel, string name, key id, string message );