Pages

Wednesday 15 June 2011

Second Life - Scripting (LSL)

During our previous session we focused on constructing virtual objects by linking multiple prims to form a single object but there's more to SecondLife objects than just creating static objects that do nothing but let Avatars sit on them. We can add interactivity to any object by embedding LSL (Linden Script Language) scripts in our objects. This post will focus on demonstrating some of the functionality available through this scripting language.

LSL syntax is very similar to javascript, but unlike javascript LSL is a strongly typed language requiring all variables to be declared to be of a specific data type prior to using.

To embed LSL to an object, open the build tool, navigate to the contents tab and create a new script.


I have listed a couple simple LSL scripts below to demonstrate the language in action.

Hello, World example

Keeping with the tradition, the following example script will output Hello, World whenever someone touches it's corresponding object.

The llsay() function accepts two parameters, an integer channel and the text, channel 0 is public anyone within the transmitting radius will see the message in their chat area. Other private channels are often used to enable communication between objects. If you want to pass messages from one object to another you can send commands on a private channel where the other object is listening on. The llsay function will transmit messages to a 20m radius around the object.


On clicking the object "touch" the following message will be displayed in the chat area



Sending an Email
The llEmail() function accepts 3 string parameters, destination email address, email title and test message.



The email below was delivered within seconds of clicking the object, the email not only contains the message passed in as a parameter but also additional details about the object such as location.

This function is often used by in-world robots which are interfacing with external system (not on SL) reporting back in-world activities. 

Move object
The llsetpos() function will change the position of the object to that defined by the vector passed as a parameter.

On clicking the corresponding object the object will move 1 meter upwards.

Conclusion
LSL scripting is very similar to nowadays's languages so syntax wise there is no learning curve, the only thing that one would need is some understanding of functions available all of which are documented at lslwiki.net. 

0 comments:

Post a Comment