February 23, 2007
Using the Linden Scripting LanguageConnecting To the World
LSL's real power is in its ability to allow an object to communicate and interact with the rest of the world. To cover everything you can do with it would require a separate book, but the following should get you started.
Chat
In our first example, we showed that an object can chat to the rest of the world using the llSayfunction. This is handy for communicating to people near the object, and it can also be useful for local object-to-object communication.
default
{
state_entry()
{
llListen(0, "", llGetOwner(), "");
}
listen(integer channel, string name, key id, string message)
{
llSay(0, message);
}
}
The preceding script is a simple chat repeater that illustrates the basics of scripted chat. All it does is repeat everything the object's owner says. When the script starts, llListen sets up the listen event so that the object can listen for chat. llListenlets you filter what you want to listen for by chat channel, name, UUID, and message.
integer llListen(integer channel, string name, key id, string message)In the example, the script listens on channel 0, which is the public chat channel that all avatars chat on. There are a few billion chat channels so that objects can chat to each other without fear of message collision. The name and message parameters are left blank in this case so the script will listen for all names and messages. llGetOwner returns the UUID of the owner, so in this case the script ends up listening for public chat made by the script's owner. When the script "hears" something that matches the requirements set in llListen, the listen event will be triggered. In this case we just pass the message that was heard to llSay. Chat sent with llSay can be heard in a radius of 20 meters; alternatively, chat can be sent with llWhisper (10 meters) or llShout (100 meters). Because there are times when you want to use chat but keep it private, there is also llOwnerSay -- a special chat that only the object's owner can hear. IMThere are times when you want to send a message to someone who is not within Whisper, Say, or Shout radius, or you want to keep your message private. The easiest way to do this is through an instant message (IM). IMs can be "heard" anywhere in Second Life, but only by the intended recipient. To send an IM, you need to know the intended recipient's UUID.llInstantMessage(key uuid, string message)If the resident is offline, the message will be saved until they log in next. Objects cannot IM other objects. SensorsSensors allow you to gather information about avatars and objects near your object. Setting up a sensor is somewhat like setting up a listen for chat. When llSensor is called, the parameters filter out the results, and if there are any matches a sensor event is called. The major difference is that whereas llListen is continuous, llSensor is a single request. The following script is an example using a sensor.default
{
state_entry()
{
//Set up a repeating sensor, that once a second looks for any
//avatars within a sphere with a 20 meter radius.
llSensorRepeat("", "", AGENT, 20.0, PI, 1.0);
}
sensor(integer detected) //A sensor returns the first 16 items detected.
{
// Say the names of everyone the sensor detects
for(i=0;i |
|
||||||||||||||||||||||||||||||
|
|
|
|