Building a Simple Elevator
As an example, this article shows how to create an elevator that can move up and down in a skyscraper in Second Life. Figure 1
shows the skyscraper to which this elevator is attached.
|Figure 1. A Skyscraper in Second Life: Here's the skyscraper to which the elevator described in this article is attached.|
You can see this elevator in action live in the game in the "Gyeonu" region at an X location of 51 and a Y location of 79. Second Life uses special URL's called "SLURLs." You can visit the elevator
directly if you've installed the SL client. If the SLURL link doesn't work for you, click the map button in the SL client, search for "Gyeonu", and then type the coordinates 95
, and 44
into the coordinate fields.
Following the link takes you to a web site that displays an overhead map of the area where the elevator is. If you have Second Life installed, you can choose "Teleport Now" to teleport to that location. The elevator allows you to travel to all of the floors of the skyscraper, as well as the roof. To use the elevator, you simply "sit on it," which is one of the built-in actions that appears on a popup menu when you right-click an object in the game. The elevator popup menu lets you choose where the elevator should go. You'll also find there's a green cone on each floor of the skyscraper that lets you call the elevator to that floor.
Two scripts control the elevator. The first script, Elevator.lsl
causes the elevator to actually move and interact with avatars (see Listing 1
). The script in Listing 2
) applies to the green elevator call cones on each floor of the skyscraper. It communicates with the main elevator script and summons the elevator car. Although the listings (and the downloadable code
) contain the full scripts, I'll walk you through the main elevator script so you can see how it responds to a sit
event and how it displays a menu.
Configuring the Elevator and Setup
You will notice some "constants" near the top of the elevator script:
integer CHANNEL = 42; // dialog channel
list MENU_MAIN = ["Floor 1", "Floor 2", "Floor 3", "Floor 4", "Floor 5", "Floor 6", "Floor 7"]; // the main menu
float BOTTOM = 27.300;
float FLOOR_HEIGHT = 10;
float SPEED = 1;
LSL does not actually support constants so these are really only "logical" constants. The values shown fit the skyscraper, but you could modify these constants to configure the elevator for your own building.
defines what channel the elevator car uses to communicate with the elevator call cones. MAIN_MENU
defines the buttons for each floor. Note that this is also how you define how many floors the elevator will visit. BOTTOM
is the z-coordinate of the ground floor of your building. SPEED
is the speed at which the elevator moves.
To setup the elevator the state_entry
event is fired. This event is fired whenever a state is entered. As you can see from Listing 1
(or the code below), the state_entry
is placed in the default state. Since the default state is the first state that a LSL script starts in, this event runs when the elevator starts up. The system fires a state_entry
event whenever an object enters a new state. Because all objects start up in the "default" state the state_entry
event for the default state always fires first when the program starts. Here's the event code:
// listen for dialog answers (from multiple users)
llListen(CHANNEL, "", NULL_KEY, "");
llSitTarget(<0,-0.5,0.5>, llEuler2Rot(<0,0,-90>) );
llSetText("Sit Here to Ride Elevator",<0,0,0>,1.0);
target = BOTTOM;
command tells the script to begin listening on the specified channel, causing the script to begin receiving events when a user calls the elevator. The llSitTarget
event allows an object to say, in x
coordinates, where a user should sit on the object. The first vector <0,-0.5,0.5>
specifies a position relative to the center of the elevator car. The second vector <0,0,-90>
specifies how to rotate the user's avatar relative to the object. This will rotate the avatar -90 degrees on the z axis. The z-axis is the axis that goes up and down. Therefore rotating on the Z axis causes the avatar to change the direction they are facing, yet keep their feet on the ground. Think of the z-axis as an imaginary like drawn from above your head to below your feet. Additionally, calling llSitTarget
allows the object to receive events when users sit on the object.
method causes an object to display instructional text above itin this case, letting users know that they should sit on the elevator to ride it. Finally, the script initializes the elevator's target z-coordinate (stored in the target
variable) to BOTTOM
the ground floor.
Displaying a Menu
When a user sits on the elevator, Second Life sends a changed
event to the elevator script. Here's the event handler code:
|Figure 2. Elevator Ride: The figure shows my avatar at the base of the skyscraper waiting for the elevator to start.|
llDialog(llAvatarOnSitTarget(), "Where to?",
method displays a simple menu to the user. You can see this menu in Figure 2
, which shows my in-game character "Encog Dod" waiting for the elevator to start.
When the user selects a floor, the listen event will fire, as shown below:
listen(integer channel, string name, key id, string message)
integer idx = llListFindList(MENU_MAIN, [message]);
if( idx!=-1 )
llSay(0,"Elevator heading to " + message + "." );
target = BOTTOM + (idx*10);
When the listen
event fires, the message
event parameter will contain the menu-item text that the user selected. The listen event first determines the index of the selected item in the MAIN_MENU
array, setting the integer variable idx
to the zero-based index of the floor the user selected. The elevator then reports that it is heading to that floor and sets the target variable. Finally, it changes the state to moving
It is possible that more than one avatar might sit on the elevator at once. In this case, Second Life presents all the avatars with a popup to choose a destination floor. The elevator does not "queue" floors like a real elevator would; instead, whichever avatar is the last to select a floor dictates where the elevator goes. However, all avatars will share the ride.