Difference between revisions of "LSL 101/The Structure of a Script"
m (→The Structure of a Script: edit) |
m (editing) |
||
Line 17: | Line 17: | ||
In order to explain even this short piece of code we need to introduce a few terms. | In order to explain even this short piece of code we need to introduce a few terms. | ||
LSL scripts use ''states'' | LSL scripts use ''states'', ''events'', ''functions'' and ''variables''. We'll use this example to show how they work together and go into details below. | ||
We'll use ''comments'' (two forward // slashes) to talk inside the script. Anything on a line after // will not execute so you can leave yourself notes like this: | We'll use ''comments'' (two forward // slashes) to talk inside the script. Anything on a line after // will not execute so you can leave yourself notes like this: | ||
Line 39: | Line 39: | ||
Always formatting like this makes it a lot easier to read and debug your code later. There is no "right" or "wrong" in the actual arrangement of white space on the page, in fact, some scripters prefer a more compact style. | Always formatting like this makes it a lot easier to read and debug your code later. There is no "right" or "wrong" in the actual arrangement of white space on the page, in fact, some scripters prefer a more compact style. | ||
==States== | ==[[State |States]]== [https://wiki.secondlife.com/wiki/State State] | ||
''State'' | A ''State'' in LSL contains events which relate to each other. For example, cars can either be moving or stopped. When the car is in *state moving* it's going forward and when it's at rest it is in *state stopped*. There may be other states (*state reverse, *state stolen, *state wrecked, *state repaired) but *state moving* and *state stopped* are probably the most common states for a car! | ||
All LSL scripts have at least one state: the ''default'' state. This is the state when no other states are active. You can see in the code above the word ''default'' is used to tell the script about what happens in the default state. | All LSL scripts have at least one state: the ''default'' state. This is the state when no other states are active. You can see in the code above the word ''default'' is used to tell the script about what happens in the default state. | ||
Line 48: | Line 48: | ||
<lsl> | <lsl> | ||
default | default // here's the default state | ||
{ | { | ||
touch_start(integer num) | touch_start(integer num) // touch it to make it work | ||
{ | { | ||
llOwnerSay("I am in state default and switching now to state happy."); | llOwnerSay("I am in state default and switching now to state happy."); | ||
state happy; | // function llOwnerSay tells the owner the message and then | ||
state happy; // switches the script to state happy | |||
} | } | ||
} | } | ||
state happy | state happy // here's where state happy starts | ||
{ | { | ||
touch_start(integer num) | touch_start(integer num) // touch again !! | ||
{ | { | ||
llOwnerSay("I was really in a happy state, but returning now to default."); | llOwnerSay("I was really in a happy state, but returning now to default."); | ||
// another message to the owner | |||
state default; // this returns the script to the default state | state default; // this returns the script to the default state | ||
} | } | ||
Line 68: | Line 70: | ||
</lsl> | </lsl> | ||
Notice the happy state needs the word ''state'', so that the script knows this describes a state rather than something else. The default state does not need the word state in front of it because it is built into lsl and is required in every script. The reason you need the word ''state'' before happy is because this is a user-created state and you have to be specific or the server will not know your intent. | |||
Next, notice the curly braces '{' and '}'. These tell the script which lines are part of the default state, which are part of the happy state, and which belong to the touch_start | Next, notice the curly braces '{' and '}'. These tell the script which lines are part of the default state, which are part of the happy state, and which lines belong to the touch_start {event handlers}. | ||
The tutorial continues with Events. | |||
Revision as of 16:45, 7 July 2012
← In the Beginning | ↑̲ LSL 101 ̲↑ | Simple Script Skeleton → |
The Structure of a Script
Here is the simplest possible valid LSL script. It doesn't actually ask the computer to do anything but all scripts have, at minimum, this structure:
<lsl> default {
state_entry() { }
} </lsl>
In order to explain even this short piece of code we need to introduce a few terms.
LSL scripts use states, events, functions and variables. We'll use this example to show how they work together and go into details below.
We'll use comments (two forward // slashes) to talk inside the script. Anything on a line after // will not execute so you can leave yourself notes like this:
<lsl> default // this is the only *state* in this script
{ //Opening bracket for default state
state_entry() // this is the only *event* in this script
{ //Opening bracket for this *event*
llOwnerSay("Hello, creator!"); // this is the only *function* inside the event
} //Closing bracket for this *event*
} //Closing bracket for default state </lsl>
Always formatting like this makes it a lot easier to read and debug your code later. There is no "right" or "wrong" in the actual arrangement of white space on the page, in fact, some scripters prefer a more compact style.
A State in LSL contains events which relate to each other. For example, cars can either be moving or stopped. When the car is in *state moving* it's going forward and when it's at rest it is in *state stopped*. There may be other states (*state reverse, *state stolen, *state wrecked, *state repaired) but *state moving* and *state stopped* are probably the most common states for a car!
All LSL scripts have at least one state: the default state. This is the state when no other states are active. You can see in the code above the word default is used to tell the script about what happens in the default state.
Here is an example with two states:
<lsl> default // here's the default state {
touch_start(integer num) // touch it to make it work { llOwnerSay("I am in state default and switching now to state happy.");
// function llOwnerSay tells the owner the message and then
state happy; // switches the script to state happy }
}
state happy // here's where state happy starts {
touch_start(integer num) // touch again !! { llOwnerSay("I was really in a happy state, but returning now to default.");
// another message to the owner
state default; // this returns the script to the default state }
} </lsl>
Notice the happy state needs the word state, so that the script knows this describes a state rather than something else. The default state does not need the word state in front of it because it is built into lsl and is required in every script. The reason you need the word state before happy is because this is a user-created state and you have to be specific or the server will not know your intent.
Next, notice the curly braces '{' and '}'. These tell the script which lines are part of the default state, which are part of the happy state, and which lines belong to the touch_start {event handlers}.
The tutorial continues with Events.