Saturday, March 15, 2008

Detailed example of a Dialog Box

Dialog boxes are handled using the llDialog command. See the wiki for more details on the arguments.

This example is a low-lag design of using llListen - it is called after touch_start is called so that the user's key can be added as a filter to the llListen call. Adding the user's key is the most efficient filter, but requires us to remember to remove the listen handle using llListenRemove. Note that we call this in two spots - in the listen() handler (a user successfully clicks Yes or No) or in the timer() handler to handle a timeout.

To handle a timeout we also have to call llSetTimerEvent in a few spots. In touch_start to start a timeout countdown, in listen to cancel the timeout if we receive a response and in timer itself to stop trigger a second timeout in th event a timeout occurs.

Note the use of llFrand to assign a random channel for the llListen and llDialog commands.



integer channel;
integer listenhandle;

default
{
state_entry()
{
channel=(integer)llFrand(10000)+100;
}

touch_start(integer total_number)
{
//This example does not handle two people touching the prim at the same time
//You should use a for loop and total_number to handle multiple clicks
if(!listenhandle) // only act on touches if not already waiting for someone to answer
{
llSay(0, "Touched by "+llDetectedName(0));
llSetTimerEvent(30); // give user 30 seconds to click
listenhandle=llListen(channel, "", llDetectedKey(0), ""); // low lag only listen for one user.
llDialog(llDetectedKey(0), "Answer Yes or No", ["Yes","No"],channel);
}
}
timer()
{
llSetTimerEvent(0);
llListenRemove(listenhandle); //stop listening
llSay(0,"Timed out.");

}
listen(integer chan, string name, key id, string message)
{
if(message=="Yes")
{
llSay(0,"Yes");
}
else if (message=="No")
{
llSay(0,"No");
}
llListenRemove(listenhandle); //stop listening
llSetTimerEvent(0);
}
}

No comments:


Official Second Life Blog