This document provides an introduction to scripting in Second Life using Linden Scripting Language (LSL). It explains what LSL is, how scripts work through states and events, and provides a basic tutorial on writing a simple "hello world" script. The document outlines common LSL concepts like functions, states, events, and how to add scripts to objects in Second Life to make them interactive.
2. Getting started in scripting in Second Life LSL stands for "Linden Scripting Language" LSL is used to script the objects you will make in Second Life. This tutorial is intended for those who have never programmed before in Second Life or elsewhere. Scripting is harder to learn than basic object manipulation, but is very rewarding once you make progress. You will begin by running the standard “Hello Avatar " script and eventually move towards making your own.
3. What is LSL? Linden Scripting Language’s structure is based on Java and C. Scripts in Second Life are a set of instructions that can be placed inside any object in the world, or any object worn by an avatar, but not inside an avatar. They are written with a built-in editor/compiler. LSL has heavy emphasis on "States" and "Events". Many real life objects have "states" A door can be "open" or "closed" and a light can be "on" or "off". A person can be "hyper", "calm", or "bored". a script will have at least one state, the default state. An event can be thought of as a "Trigger". Events are predefined in LSL. Touch_start(), will trigger the code in it when the object having the script is touched.
4. WHAT CAN I DO WITH SCRIPTS? Scripts can make an object: Move Listen Talk Operate as a car or gun Change color, size or shape Talk to you Talk to another.
5. WHAT CAN I DO WITH SCRIPTS? "Prim" or primitive, the basic building block can have a script. When several prims are linked, they can each contain a script which speaks to the rest of the object via Link Messages. Here we focus on single scripts in a single prim. If you've built in Second Life, everything you can define in the edit window can be defined in a script. All interaction you see between objects or between avatars and objects is via scripts. Learning more about the world and building model is vital to some aspects of scripting
6. Running Your First Script Traditionally one starts by writing the smallest program to print "hello world“. Since LSL only runs inside objects, you must know how to make an object and put a script inside it. You must be on land which allows building. In the edit window you may five tabs marked general, object, features, content, and texture. Click "content".
7. Running Your First Script 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. 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. GOOD! You have compiled and run your first LSL script!
8. The script: When I am in the default state, and I am touched, say "Hello World" on channel zero".
9. Write, run, RE-write Most scripts you make won't run the first time you run them. When you hit "save" on a script, the LSL editor "compiles" the code to form LSL can understand. It stops if it finds an error. Brackets, parenthesis, and semicolons must all be perfectly in place If you are new to programming this can be frustrating Part of a programming in ANY language is learning how to precisely define steps and correctly type them into the language you are working in. Thus you will find yourself writing, running, then RE-writing your code several times. The script you made runs the instant you hit save. If you take it into inventory, it will "suspend" what it was doing but go right back to it when rezzedagain Each time you re-write your code you must save the script.
11. 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.
12. EVENTS Events are inside of states. When a state is active, those events wait to be triggered and run the code inside them. "state entry" which is trigged by the a state being entered "touch start" which is triggered when you, or anyone, touches an object. Lets take a look at the default code. default // state{ touch_start(integer total_number) // this is an event { // this is the content of the event } // end of event } // end of state
13. 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. ex. llSay() 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. llSay(0, "Touched.");
15. After saving your script occurs following: The instant you save your script, it enters default state, which in turns runs the "state_entry" event which in turn runs the function llSay() which makes the object talk. After this the program waits idle in the default state until a new event is called. Touching the box triggers the even "touch_start" which also makes the object speak.
16. Introducing States and Events LSL scripts will not run beginning to end . Instead they will look for a default state and wait for an event. Within those events, there can be a call to go to a new state. Lets look at a script with two states with two events in each.
17.
18. A simplification of this would be 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".
19. A closer look llSay(0, "turning on!"); Channel zero is the channel you see all public chat on. A semicolon ends the line. llSetColor(<0,0,0>, ALL_SIDES); This turns the prim to it's brightest tint. You see it as bright white . The three 0's stand for the black and the three 1's stand for the white.
20. Program creates a loop 1. Enters default state 2. Runs code in "state entry" 3. Waits to be touched. 4. When touched enters "state off" 5. Enters "state off". 6. Runs code in "state entry" in the "off" state's body 7. Waits to be touched in the "off" state's body 8. When touched enters "default" state. Where the whole thing starts over.
21. Objects speaking is a great way to know what a script is doing As you get into more complex scripts this can get pretty noisy to the surrounding people! llWhisper( ) is just like llSay( ) but only broadcasts at half the distance. llWhisper(0,"turnign on!"); //might work a bit to save the sanity of your neighbors. Using llShout( ) doubles the distance heard, but can cut the amount of friends you have in SL. llOwnerSay( ) uses no channel and is heard only by you.llOwnerSay("turnign on!");
22. Totally silent message via llSetText( ) You can make a totally silent message via llSetText( ) like this. llSetText("I am on", <1,1,1>,1.0); <1,1,1>, means "white" and <0,0,0> means "black". Replace the llSay(0,"turnign off!"); with... The 1.0 is the alpha setting. 1.0 means fully opaque, and 0.0 would be completely transparent (invisible). Read about programming in SL wiki. http://wiki.secondlife.com/wiki/SL_Cert_-_Basic_Scripting