You’re probably wondering what you can do with LSL, and how quickly you can do it. We’ll start with some simple examples, disect them, and introduce you the the script development process while we’re at it.
2.1. Hello Avatar
Continuing a long tradition of getting started by looking at a script that says “Hello”, we’ll do just that. Though obviously not a particularly useful example on it’s own, this example will introduce us to:
• Creating a basic script
• Script states
• Calling functions
• Script events
• Applying a script to an object
2.1.1. Creating the Script
Start by opening your inventory and selecting ’Create|New Script’ from the inventory pull down menu. This wil create an empty script called ’New Script’ in your ’Scripts’ folder. Double click on the text or icon of the script to open the script in the built in editor. When you open the script, the viewer will automatically insert a basic skeleton for lsl. It should look like:
default
{
state_entry()
{
llSay(0, “Hello, Avatar!”);
}
touch_start(integer total_number)
{
llSay(0, “Touched.”);
}
}
A casual inspection of this script reveals that this scipt probably says ’Hello, Avatar!’ when it enters some state, and it says ’Touched.’ when it is touched. But since this is also probably the first time you have seen a script we’ll disect this short listing, explaining each segment individually.
2.1.2. Default State
default
{
…
}
All LSL scripts have a simple implicit state machine with one or more states. All scripts must have a default state, so iff there is only one state, it will be the ’default’ state. When a script is first started or reset, it will start out in the default state.
The default state is declared by placing the default at the root level of the document, and marking the beginning with an open brace ’{’ and ending with a close brace ’}’. Because of it’s privileged status, you do not declare that it is fact a state like you normally would with other states.
Every time you enter a state, the script engine will automatically call the state_entry() event and execute the code found there. On state exit, the script engine will automatically call the state_exit() event before calling the next state’s state_entry handler. In our example, we call the llSay() function in state_entry() and do not bother to define a state_exit() handler. the state entry and exit handlers are a convenient place to initialize state data and clean up state specific data such as listen event callback.
You can read more about the default state, and how to create and utilize other states in the states chapter.
2.1.3. Functions
The language comes with well over 200 built in functions which allow scripts and objects to interact with their enviornment. All of the built in functions start with ’ll’.
The example calls the ’llSay()’ function twice. llSay() is used to emit text on the specified channel.
llSay( integer channel string text );
Say text on channel. Channel 0 is the public chat channel that all avatars see as chat text. Channels 1 to 2,147,483,648 are private channels that aren’t sent to avatars but other scripts can listen for.
You can define your own functions as long as the name does not conflict with a reserved word, built in constant, or built in function.
2.1.4. Touch Event
touch_start(integer total_number)
{
llSay(0, “Touched.”);
}
There are many events that can be detected in your scripts by declaring a handler. The touch_start() event is raised when a user touches the object through the user interface.
2.1.5. Try it Out
Now that we have seen the default script, and examined it in some detail, it is time to to see the script in action.
Save the script by clicking on Save. During the save process, the editor will save the text of the script and compile the script into bytecode and then save that. When you see message ’Compile successful!’ in the preview window, you know the compile and save is done.
To test the script you will have to apply it to an object in the world. Create a new object in the world by context clicking in the main world view and selecting Create. When the wand appears, you can create a simple primitive by clicking in the world. Once the object appears, you can drag your newly created script onto the object to start the script.
Soon after dragging the script onto the object, you will see the message Object: Hello Avatar!
Make sure the touch event is working by clicking on the object. You should see the message Touched printed into the chat history.
2.2. Using The Built-In Editor
The built in editor comes with most of the typical features you would expect from a basic text editor. Highlight text with the mouse, or by holding down the shift key while using the arrow keys. You can cut, copy, paste, and delete your selection using the ’Edit’ pull down menu or by pressing the usual shortcut key.
2.3. Using Alternative Editors
Since the built-in editor supports pasting text from the clipboard, you can employ a different editor to edit your scripts, copying them into Second Life when you’re ready to save them.