Basics of scripting

This is, as the title suggests, no more than an overview of the basics of scripting in Linden Scripting Language, intended solely to give a feel for the language.  Links to third-party tutorials and scripting resources are published elsewhere on vortiCISM.

Using an editor

new_scriptYou’ll first want to consider whether you’d want to do your scripting using the in-world editor or using an offline editor.  You’ll probably use both, though for different purposes: an offline editor for writing the bulk of your programs, the in-world editor for ‘quick-and-dirty’ short scripts and for in-world tweaking of uploaded scripts.  You can create a new script in-world by selecting Create>New Script from the menu bar at the top of your inventory (this will insert a new default document in the Scripts folder of your inventory) or by clicking ‘New Script’ in the Content tab of the Object Editor (see LSL basics, below).

Offline, you have several choices.  Windows users might opt for the free LSL-Editor, while Mac users may install a LSL plugin (there are several) in the free TextWrangler editor.  This tutorial, however, will be mostly based on the open source and cross-platform (Linux, Mac, Windows) Eclipse IDE with the LSL Plus plugin as, in my opinion, the best choice.

Using Eclipse

Download and install Eclipse (I recommend Eclipse Classic); launch the application.

Click for larger image

Click picture for larger image

You can install the LSL Plus plugin directly from the Internet.  Navigate in your web browser to the Update Sites page of the LSL Plus web site and copy the URL of the nearest site to your clipboard (for my Kingston University students this will be http://lslplus.sourceforge.net/update-sites/europe-uk).  In Eclipse, select ‘Software Updates …’ from the Help menu and, under the ‘Available Software’ tab, click the ‘Add site …’ button.  Paste the update site URL into the location field and click OK.  The update site shows the latest available plugins, the first of which will be the basic Lslplus GUI followed by platform-specific portions beginning with Lslplus_part_.  Select the basic plugin and the correct portion for your platform and install.  Once the installation of the plugin is complete you will need to re-launch Eclipse in order to use it.

For more detailed information on how to install LSL Plus, go to the Download/Install page of the LSL Plus web site.

LSL basics

There are many good web-based and in-world tutorial introductions to Linden Scripting Language (LSL), so this document is intended as no more than a very basic overview to get you started (and hopefully to assuage the nervousness any non-programmers may feel!)  I’ve listed a number of resources and tutorials in the Help section of this site; you can also learn the basics of scripting from the Scripting Guide and Scripting Portal accessible from the Help menu in your Second Life browser, though these are more reference documents than tutorials.

default_script.pngLSL is an event-driven state-based language. (If you already know something about state machines, you’ll get the idea quickly.) Scripts can be embedded in prims–in fact, may be embedded in any object other than an avatar–and, responding to events (for example, some action, such as a click, on the part of a user), determine the behaviour of that object: its transition from one state to a successor state.

When you create a new script (by selecting ‘New Script’ from the Create menu in your inventory or from the Content tab of the Object Editor) you’ll find there is already a basic ‘generic’ script created for you (see screenshot).

The basic script consists of a state, named default (i.e., the state it always starts in and to which it returns when reset), which contains two events: state_entry and touch_start. Both events contain a built-in function, llSay, which takes two arguments: the number of a chat channel to speak on (in this case, channel 0, the public chat channel that all avatars see as chat text) and something to say on that channel. In the case of the ‘generic script’, the prim in which it is embedded will (rather pointlessly) say “Hello, Avatar!” in its default state and, when clicked, say “Touched”.

Those of you already familiar with Perl or C or Javascript (or any one of a number of other programming and scripting languages) will recognise the syntax: statements are enclosed within braces {…} and separated by semi-colons; functions are written as function_name(arg1, …, argn). Don’t let this scare you off … LSL is much easier to learn than C.

To begin with, an introduction to the basic building blocks of LSL: constants, variables, and integers …

Constants

LSL provides a large number (currently 386) of built-in constants.  Some are Booleans (TRUE, FALSE), some mathematical (PI, DEG_TO_RAD, …), others represent special values for built-in functions.  We shall look at uses of some specific constants later in this tutorial.

Variables

Variables store information that will be used in a script. There are a half-dozen variable types, which I list here (and, where necessary, comment and exemplify later):

  • string. A sequence of characters (text, for example)
  • key. A UUID (see Glossary)
  • integer. A whole number in the range -2,147,483,648 to 2,147,483,647
  • float. A decimal number in the range 1.175494351E-38 to 3.402823466E+38 (don’t ask … just trust me!)
  • vector. Three floats <x, y, z>, usually a position, colour, or Euler rotation
  • rotation. A quaternion rotation, composed of four floats <x, y, z, s>
  • list. A heterogeneous list of other data types

LSL is ‘strongly typed’, and all variables used in a script must be declared before then are used. Script-level variables are declared at the head of the script, and are globally accessible; local variables are declared within functions, its scope only that function.

For example:

integer i;

declares a variable i to which an integer (and only an integer) can subsequently be assigned, using the equals operator:

integer i = 0;

while string declares a variable to which a string can therefore be assigned:

string my_string = “Khoisan Fisher”;

and the value of the variable can always be updated. For example:

i = i + 1;

or, for short:

i += i;

Or, if the value of i is to be incremented by only 1 each time:

i++;

Functions

User-defined functions, called within states, must be declared outside of states

Example 1: simple script with locally scoped variable

The following script, embedded in a door, will announce the name of the avatr who has clicked on the door

default
{
    // no action associated with start state
    state_entry()
    {
    }

    // when the door is clicked
    touch_start(integer a)
    {
            // get the name of the avatar and assign it as the
            // value of the string sName, then say the name
            // to public chat
            string sName = llKey2Name(llDetectedKey(0));
            llSay(0, sName + " is at the Door");
    }
}

Example 2: script with simple user-defined function

A simple script, with user-defined function display() that will increment an integer by 1 each time the object it’s embedded in is clicked:

// declare variable
integer count;

// define function to be called in default state
display()
{
     llSay(0, "Total count is: " + (string)count);
}

default
{
     // start state
     state_entry()
     {
          // initialise variable
          count = 0;
     }

     // event (clicking the object) that will change state
     touch_start(integer total_number)
     {
          // increment by 1 the value of variable 'count'
          // each time the object is clicked
          count ++;
          // and then display in local chat the current value of
          // the variable
          display();
     }
}

Summary

Characteristically a script will have the following structure (optional elements in parentheses):

Finally …

I’ve customised an 8-station tourHUD for LSL scripting, the first five locations providing scripting tutorials, with large libraries of free scripts in the sixth and seventh, and Jeff Heaton’s tutorials in the last. The tourHUD is available from the main CISM building.

scripting_tourhud.png

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s