Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Programming Second Life with the Linden Scripting Language : Page 3

If you learned programming in your first life, you can now exercise your skills in Second Life, using Linden Scripting Language to activate objects you build in the popular simulation.


advertisement
Moving Objects
There are two ways to move an object in Second Life. You can move objects physically, using thrust, or directly, using x, y, z coordinates. Moving objects directly is somewhat easier than moving them physically, so I'll focus on direct coordinate-based movement here. Fortunately, that's rather simple for an elevator because it moves in only one dimension: up and down. As you'd expect, in Second Life, that's the z-coordinate.

When the elevator enters the moving state, the elevator will begin moving. The moving state begins when its state_entry event fires.

state moving { state_entry() { llSetTimerEvent(0.1); } ...

The state_entry event registers a timer that "ticks" ten times per second. Each tick causes the timer event to fire. The code in the timer event causes the elevator to move. The timer begins by getting the current position of the elevator as follows.

timer() { vector pos = llGetPos(); ...

If the z-coordinate is not at the desired target then the script moves the elevator either up or down towards the target. The SPEED constant controls how fast the elevator will reach its target.



if( pos.z!=target ) { if( pos.z>target ) { pos.z = pos.z - SPEED; } else { pos.z = pos.z + SPEED; } }

This continues with each timer tick until the position check determines that the elevator is very close to the target—less than the distance covered by the SPEED constant. In that case, the script moves the elevator exactly to the target, stops the timer, notifies the user, and returns the elevator state to the default state.

if( llFabs(pos.z - target) < SPEED ) { pos.z = target; llSetTimerEvent(0); llSetPos(pos); llSay(0,"Elevator has reached its target." ); state default; }

Otherwise, the elevator is not close to its target and must continue moving, so the timer script moves the elevator to the new calculated position.

llSetPos(pos);

In addition to being controlled by users sitting on the elevator, the elevator can also be called to a specific floor by one of the green elevator-call cones in the skyscraper, which requires communication between objects.

Communicating Between Objects
Objects in Second Life communicate with each other in almost exactly the same way that avatars in Second Life communicate. Avatars can "say" something that will be heard by nearby avatars; they can also shout something for greater distance. Everything said in Second Life uses a specific channel. Avatars normally speak on channel zero. If an object were to listen on channel zero it would hear nearby users speaking! Similarly, if an object speaks on channel zero, nearby users will hear it speak.

On each floor of the skyscraper there is a green cone that will call the elevator. The cones are very simple. They simply call the elevator by transmitting which floor the elevator has been called to. This is done in exactly the same manner as the elevator's menu.

Each of the floor cones contains a touch_start event, fired whenever a user touches the cone. Here's the touch_start event for the first floor cone.

touch_start(integer total_number) { llShout(CHANNEL, "Floor 1"); }

Note that the script shouts the message. This gives it greater distance than the llSay method, which is necessary because the skyscraper may be very tall. The elevator will pick up this communication with its listen event and begin traveling to the specified floor.

In this article you saw how to create an elevator in Second Life. This is only the beginning. There are many methods in the LSL library to allow you to create an almost endless array of in-game objects. You can create objects to perform commerce and sell other objects. You can create weapons for games within Second Life. Your objects can even interact with external web servers, letting you use external resources in your scripts.

If you are interested in learning more about LSL, Linden Labs maintains a Wiki about LSL programming where you can find more information.



Jeff Heaton is an author, college instructor, and consultant. Jeff is the author of four books and over two dozen journal and magazine articles. Jeff maintains a personal website where he publishes information about artificial intelligence, spider/bot programming, and other topics.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap