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 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. “event execution model”(unique in this way) An event can be thought to be a "Trigger". Events are predefined in LSL. Touch_start(), will trigger the code when the object is touched.
4. Linden Scripting Language “LSL” makes objects interactive Allows commerce, serious gaming, interfaces, web connectivity Linked objects may perform independent actions
5. 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.
6. 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
7. 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, featurestexture and content,. Click "content".
8. 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 program window) You should see the words "Hello Avatar" from "object“ If you touch the object, it will say "Touched.“ (make sure the "edit" window is closed for touching to work. “ GOOD! You have compiled and run your first LSL script!
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 SL 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.
10. A Closer Look The script: When I am in the default state, and I am touched, say “Touched" on channel zero". The script: By start say “Hello, Avatar" on channel zero".
11. STATES A "State" in LSL is a section that is running, and waiting for events. Only one state can be active. Every script must have a default state with at least one event in it. Except for the default state, each state is defined 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 from 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 llOwnerSay( "turning on!"); Object speaks to primitive owner! A semicolon ends the line. llSetColor(<1,1,1>, 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.
21. 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.
22. 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.
23. 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).
38. Variables what are then? Variables hold data They come in flavors (types) Have meaning to the ENTIRE script or only a portion of it Using them properly is one of the most important thing in scripting
39. Variable Types Float = floating point or real number Integer = positive whole number String = a text word or phrase Vector = a set of three floats rgb color, xyz pos, xyz vel, xyz accel Key = uuid - object identity Rotation = x,y,z,s - hard! List = rough database Typecasting a=(string)b
40. Global vs Local Variables Declare global variables at top of the script - they are usable throughout Declare local variables inside the block it will be used in
41. Loops for (j = 0; j < count; j++) { // do stuff here } do { // do stuff here and increment j } while (j < count); while (j < count) { // do stuff here and increment j }