Getting Started in LSL Scripting
Getting Started in LSL Scripting
This window shows the contents of an object which can hold scripts,
notecards, even other objects. Press "new script" to add a new script.
This will open the LSL editor with a default script. This editor will color code
your syntax and provide some info about keywords when you hold your mouse
over them. It will also do basic syntax checking.
Before explaining the code, lets run it. Hit "save" and close your edit window
(not the LSL editor window).
You should see the words "Hello Avatar" from "object"
If you touch the object, it will say "Touched."
(make sure the "edit" building window is closed for touching to work.
Congratulations! You have compiled and run your first LSL script!
Any line starting with two forward slashes is a comment. It will not run and is
used to help you document your code.
// This is a comment
STATES
A "State" in LSL is a section that is running, and waiting for events. Only one
state can be active at any one time per script. Every script must have a default
state with at least one event in it. Except for the default state, each state is
define by the word STATE followed by the name of the state. The contents of
the state are enclosed in two curly brackets.
default
{
// contents of state go here
}
state playing
{
// this is a state called "playing"
}
EVENTS
Events are inside of states. By "inside" I mean it is between the open and
closed curly brackets that represent the body of the state. When that state is
active, those events wait to be triggered and run the code inside them. We've
seen "state_entry" which is trigged by the a state being entered, and
"touch_start" which is triggered when you, or anyone, touches an object.
Lets take a look at the deafult code.
// Code start
default
{
touch_start(integer total_number) // this is an event
{
// this is the content of the event
}
// end of event
}
// end of state
FUNCTIONS
Functions lay inside of events and are either defined by you or built-in. Those
built in to LSL all start with two lower case L's. We've seen llSay() so far.
Functions take "arguments" or values in the parentheses that follow it. If you
hover over the function in the editor, a popup will show that tell you what the
function is expecting. In the case of llSay it expects a number and a string. We
send it the number zero and the string "Hello, Avatar!" separated by commas.
The function is "expecting" a number and strings and won't take anything else.
Touching the box triggers the even "touch_start" which also makes the object
speak.
default
{
//set color to light and, if touched, enter the "off" state.
}
state off
{
//set color to dark and, if touched, enter the "default" state.
}
Note that after "default" all new states begin with the word "state". Also, while
the object has a texture, the color will effect the "tint" more than the true color.