Difference between revisions of "LSL 101/Simple Script Skeleton"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
(33 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{LSL Wikibook Index}}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:
[[Category:LSL 101]]
{{NavNextPrev|prev=The Structure of a Script|next=Comments, White-space and Formatting}}
{{LSL Wikibook Index}}
Here is an example of 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>
<source lang="lsl2">
default
default  
{
{
     state_entry()
     state_entry()  
     {
     { // state_entry event handler goes here
     }
     }
}
}
</lsl>
</source>


In order to explain even this short piece of code we need to introduce you to some terms that will probably be new to you.
Looking at a script, the first thing you might notice is that the ratio of punctuation to words is quite high.  This is a general characteristic of LSL.  Along with parenthesis and curly braces, there will be lots of semicolons, quotation marks and square brackets, and it all has to be done just right.  Fortunately, the rules for using punctuation are much simpler than for a natural language.  So even though you are likely to struggle a lot with punctuation at the beginning, that phase won't last long.


LSL scripts use a concept called ''states'' and ''events''.
In addition to the punctuation, there are two "names" in this example, "default" and "state_entry".  In LSL, the underscore is not a punctuation mark, but one of the characters that can be used in names.  It's most often used to combine what might be multiple words in English into a single name, because LSL requires every name to be a single word.


''State'' is actually a very good name for what states do in LSL. If you think of a car, it can either be moving or stopped. We can say its state is moving (when it is moving) and its state is stopped (when it is stopped). Another example is your own state of being: you can be awake, asleep, active, sitting, standing, hungry, bored, confused, etc.
Based on this example script snippet, you might guess that there is a recurring pattern of the form
 
<source lang="lsl2">
All LSL scripts have at least one state: the ''default'' state. This is the state when no other states are activeYou can see in the code above the word ''default'' is used to tell the script about what happens in the default state.
heading
{
    ...
}
</source>
in LSL, and you would be absolutely right. Each one of these is called a <b>block</b>, and a large part of an LSL script is made up of blocks within blocks (within blocks ...)Since getting the punctuation just right is mandatory, it is a good idea to vertically align opening and closing curly braces with their heading, and indent everything in between.


Here is an example with two states:
This is all grammar; what about the <u>meaning</u> of the two blocks? 


<lsl>
The outermost block, with the heading <b>default</b> denotes something referred to as the <b>default state</b>. States are a unique aspect of LSL, and we'll cover them later. But it will be easier to understand just how they work when you know more about other aspects of LSL. So for now, you can regard the outer block to be just a grammatical structure that is required in all scripts.
default
{
    state_entry()
    {
        llOwnerSay("Switching to the running state...");
        state running;
    }
}


state running
The inner block, headed by <b>state_entry()</b>, is an <b>event handler</b> block.  Unlike states, understanding events and event handlers is crucial from the very start.  In fact, if you happen to have had some prior experience with programming, it's <u>especially</u> important that you grok(understand) events, because unlike a traditional computer program, events occurring outside of your script determine when the various parts of your script get executed.
{
    state_entry()
    {
        llOwnerSay("This script is now running");
    }
}
</lsl>


The first thing to notice is that the running state needs the word ''state'', so that the script knows this describes a state rather than something else.  The default state does not need to be proceeded by the word state.
The state_entry event is a pretty simple one.  It occurs when the script first starts running.  Typically, this will be when you add a new script to a prim's inventory or save your changes after editing a script that is in a prim's inventory. When the sim server detects the state_entry event, it executes any commands (and only those commands) that occur between the { and } of the state_event handler block.


Next notice the curly braces '{' and '}'.  These tell the script which lines are part of the default state, which are part of the running state, and which belong to the state_entry() event handler (which we describe next).
'''Click on [[LSL_101/Comments, White-space and Formatting | Comments, White-space and Formatting]] to continue.'''

Latest revision as of 12:45, 24 January 2015

← The Structure of a Script ↑̲  LSL 101  ̲↑ Comments, White-space and Formatting →

Here is an example of the simplest possible valid LSL script. It doesn't actually ask the computer to do anything but all scripts have, at minimum, this structure:

default 
{
     state_entry() 
     { // state_entry event handler goes here
     }
}

Looking at a script, the first thing you might notice is that the ratio of punctuation to words is quite high. This is a general characteristic of LSL. Along with parenthesis and curly braces, there will be lots of semicolons, quotation marks and square brackets, and it all has to be done just right. Fortunately, the rules for using punctuation are much simpler than for a natural language. So even though you are likely to struggle a lot with punctuation at the beginning, that phase won't last long.

In addition to the punctuation, there are two "names" in this example, "default" and "state_entry". In LSL, the underscore is not a punctuation mark, but one of the characters that can be used in names. It's most often used to combine what might be multiple words in English into a single name, because LSL requires every name to be a single word.

Based on this example script snippet, you might guess that there is a recurring pattern of the form

heading
{
     ...
}

in LSL, and you would be absolutely right. Each one of these is called a block, and a large part of an LSL script is made up of blocks within blocks (within blocks ...). Since getting the punctuation just right is mandatory, it is a good idea to vertically align opening and closing curly braces with their heading, and indent everything in between.

This is all grammar; what about the meaning of the two blocks?

The outermost block, with the heading default denotes something referred to as the default state. States are a unique aspect of LSL, and we'll cover them later. But it will be easier to understand just how they work when you know more about other aspects of LSL. So for now, you can regard the outer block to be just a grammatical structure that is required in all scripts.

The inner block, headed by state_entry(), is an event handler block. Unlike states, understanding events and event handlers is crucial from the very start. In fact, if you happen to have had some prior experience with programming, it's especially important that you grok(understand) events, because unlike a traditional computer program, events occurring outside of your script determine when the various parts of your script get executed.

The state_entry event is a pretty simple one. It occurs when the script first starts running. Typically, this will be when you add a new script to a prim's inventory or save your changes after editing a script that is in a prim's inventory. When the sim server detects the state_entry event, it executes any commands (and only those commands) that occur between the { and } of the state_event handler block.

Click on Comments, White-space and Formatting to continue.