jeffheaton's picture

    Objects in Second Life can communicate in many of the same ways that avatars communicate. Objects listen to conversations going on around them. Objects can also speak and participate in those conversations. Additionally, objects can send instant messages to avatars. However, instant messages between an avatar and an object are one-way. An avatar cannot send an instant message back to an object.

    The following script demonstrates how an object can listen to conversations going on around it. The object will wait for someone to say either “hello” or “goodbye”. Once the object detects either of these words, the object makes an appropriate greeting to the avatar that spoke to the object.

integer CHANNEL = 0;

default
{
    state_entry()
    {
        llListen(CHANNEL, "", NULL_KEY, "");
    }
    
    listen(integer channel, string name, key id, 
      string message) 
    {
        if( llToLower(message) == "hello" )
        {
            llSay(CHANNEL,"Hello " + name );
        }
        else if( llToLower(message) == "goodbye" )
        {
            llSay(CHANNEL,"Goodbye " + name );
        }
    }
}

    For an object to begin listening, the object must call the llListen function. This function specifies the channel the object would like to listen on. The above script calls the llListen function in the state_entry event handler. The script specifies that it would like to listen to the channel specified by the CHANNEL variable. The Linden Scripting Language does not have user defined constants. As a result, the above declaration of CHANNEL is as close as we can come to a constant.

    Channel zero is the normal conversation channel in Second Life. All communication between avatars is on channel zero. Therefore, by requesting to listen on channel zero, the object will be notified anytime something is said near the object.

    The above script contains a listen event handler. This event handler is called each time something is said near the object. The object checks for either “hello” or “goodbye”. Because the strings are converted to lower case, the user could also enter “Hello” or any mixture of upper and lower case characters. The script responds with a greeting directed to the avatar's name. The avatar's name was passed in as a parameter named name.

    The llSay function is used when a script wants to say something. The above calls to llSay use channel zero. However, often objects want to communicate with each other, and not allow nearby avatars to listen. To do this, the script should specify a channel other than zero. Many recipes in this book communicate on channels other than zero.

    In addition to llSay, two other functions allow a script to talk. The only difference between the three communication functions is the distance they cover. Table 1.1 summarizes the communication functions.

Table 1.1: Communication Distances

Communication Function Distance
llWhisper 10m
llSay 20m
llShout 100m

    There is a fourth communication function, with unlimited range. The llInstantMessage function allows an instant message to be sent to the specified avatar.

default 
{
    touch_start(integer total_num)
    {   
	// get the key of the objects owner.
    	key owner=llGetOwner();  
        llInstantMessage(owner,llKey2Name(owner)+", " 
        + (string)total_num +" Avatar(s) touched me!");
    }
}

    The above script sends a message to the object's owner every time the object is touched. It is also possible to send a message to the object's owner by using the llOwnerSay function. However, llOwnerSay does not have the unlimited distance of a llInstantMessage function call.


Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.