Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Parallel

Using the Linden Scripting Language


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).

Figure 8.5: Giving and taking money.

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;

Figure 8.6: Object 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.

Figure 8.7: A scripted airplane in flight.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.