jeffheaton's picture

    Objects can send instant messages directly to an avatar. It is currently impossible for objects to receive instant messages, either from avatars or other objects. Instant messages are sent using the llInstantMessage function. However, there is a shortcut for sending an instant message to the owner of an object. To send an instant message to the owner of an object, the llOwnerSay function should be used.

    However, to send an instant message to someone other than the owner, the llInstantMessage function must be used. Listing 6.4 shows a simple pager that uses llInstantMessage.

Listing 6.4: A Simple Pager

string name = "";
string last_online = "";
key nameKey = NULL_KEY;

default
{
    on_rez(integer p)
    {
        llResetScript();
    }

    state_entry()
    {
        llSetText("Online Detector\nTouch to Claim",<1,1,1>,1);
    }

    touch_start(integer total_number)
    {
        if(name == "")
        { 
            nameKey = llDetectedKey(0);
            name = llDetectedName(0);
            llSetText(name + "\Touch to page:\n" + name,<1,1,1>,1);
        }
        else if(llDetectedName(0) != name)
        { 
            llInstantMessage(nameKey, llDetectedName(0) + " is paging you from " + llGetRegionName());
            llWhisper(0,"A message has been sent to " + name);
        }
    }

}

    The script begins by defining several script level variables. Specifically, the name and the key for the user, who has claimed the pager, are stored. A key is a number that represents an object or avatar in Second Life. The key will be obtained when an avatar touches the pager. The first avatar to touch the pager claims that pager and will be sent instant messages when others touch it.

string name = "";
key nameKey = NULL_KEY;

    This script has one single state, the default state, that all scripts have.

default
{

    Because the object is claimed, it should be reset whenever it is rezzed back to the world. Thus, if an avatar who has claimed this object gives the object to a second avatar, the object will go unclaimed as soon as it is moved back to the world from the user's inventory.

    on_rez(integer p)
    {
        llResetScript();
    }

    When the object first starts up, text is displayed above the object instructing avatars to touch the object to claim it.

    state_entry()
    {
        llSetText("Online Detector\nTouch to Claim",<1,1,1>,1);
    }

    If an avatar touches the pager, then either that avatar is going to send a message or claim the pager. If no one has claimed the pager yet, the touching avatar claims it. If no one has claimedMark Biss2007-12-12T00:00:00Something needed in here? then use llDetectedKey and llDetectedName to obtain the name of the avatar who has claimed the pager. These functions return the name and key of the avatar that touched the object.

    touch_start(integer total_number)
    {
        if(name == "")
        { 
            nameKey = llDetectedKey(0);
            name = llDetectedName(0);
            llSetText(name + "\Touch to page:\n" + name,<1,1,1>,1);
        }

    If the pager was already claimed, send a message to the pager's owner to tell them that they were paged. Also include the region name where the pager was located.

        else if(llDetectedName(0) != name)
        { 
            llInstantMessage(nameKey, llDetectedName(0) + " is paging you from " + llGetRegionName());
            llWhisper(0,"A message has been sent to " + name);
        }
    }
}

    It is possible for two avatars to touch the object at exactly the same instant. This is a rare occurrence and most scripts in Second Life do not support it. If two avatars did touch the object at exactly the same instance, the total_number parameter to the touch event would be greater than one. To support this, llDetectedName and llInstantMessage functions both accept a parameter to specify which avatar should be detected. The script above only supports one avatar touching the script at once, so a zero is passed in.

    Supporting more than one avatar is relatively easy. Loop up to the number specified by total_number and process each avatar by calling a function such as llDetectedName for each. Of course, this does not always make sense for a script. The above script can only be claimed by one avatar at once. As a result, concurrent touches are not supported.


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