Connecting 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.
IM
There 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.
Sensors
Sensors 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<detected;i++) { llSay(0, llDetectedName(i)); } } }
Pay
Scripts can also give and take L$ (Figure 8.5).
This is handy for creating vendors, gambling games, and more. In order for an object to accept money, the script must have a money event:
default { money(key giver, integer amount) { llSay(0, "Thanks for the " + (string)amount + "L$ donation!"); } }
Giving money via a scripted object is a bit more complicated. The script needs to request permission from the object's owner to debit L$ from their account.
default { state_entry() { llRequestPermissions(llGetOwner(), PERMISSION_DEBIT); } touch_start(integer number_touching) { llGiveMoney(llDetectedKey(0), 1); } }
The preceding script gives L$1 to whomever clicks on the object that's using llGiveMoney. This script will not work unless when the llRequestPermissions function is called, the owner grants permission to let the object give money.
Inventory
An object has an inventory (Figure 8.6) -- this is where the script lives, but it can also contain most of the things a resident's Inventory can. The script can give, take, and use inventory. For instance, a gun would need llRezObject to shoot a bullet. A vendor would use llGiveInventory to give a single item, or llGiveInventoryList to give a folder. The following is an example of a drop box that shows how to give and take inventory;
default { state_entry() { //Allows anyone to drop inventory llAllowInventoryDrop(TRUE); } touch_start(integer number_touched) { //Only the owner can take it out. if(llDetectedKey(0) == llGetOwner) { //Make a list of all the objects in the inventory list contents = []; integer I; for(i=0;i<llGetInventoryNumber(INVENTORY_OBJECT);i++) { contents += llGetInventoryName(INVENTORY_OBJECT, i); } //Give all the objects in a folder llGiveInventoryList(llGetOwner(), "Drop Box", contents); } }
Physics and More
Second Life has a server-side physics simulation, which means objects will fall, bounce, and collide correctly. A script can change how the built-in physics affect an object. By default, an object in Second Life is "pinned." This means that if you lift it up and let go, it will not fall down. To make it fall, you can set its status to physical:
default { state_entry() { llSetStatus(STATUS_PHYSICS, TRUE); } }
An object can also change its collision status by becoming "phantom" with the llSetStatus function:
llSetStatus(STATUS_PHANTOM, TRUE);
Applying Forces
By changing the forces on a physical object, you can control how it moves. There are several ways to do this; the easiest way is to use llSetForce or llApplyImpulse. These work just like your high-school physics class taught you: a force is a continuous push, and an impulse is a single, instant push. A simple example:
default { touch_start(integer touched) { // bounces the object up in the air. llApplyImpulse(<0,0,100>, FALSE); } }
Vehicles
For more control, there is a suite of functions that let you change over 20 parameters to alter every aspect of how a physical object moves, from buoyancy to friction (Figure 8.7). The vehicle code is much more complex but correspondingly more powerful. For more information, check out the vehicle page at http://lslwiki.com/lslwiki/wakka.php?wakka=vehicles.