Getting Started in LSL Scripting in Second Life
Getting Started in LSL Scripting in Second Life
Hello and welcome to "Getting started in LSL scripting". LSL stands for "Linden Scripting Language" and is used to script the objects you will encounter and make in Second Life. WHO H!S " O#!$L !S %O#& his tutorial is intended for those who ha'e ne'er programmed before( Second Life or elswhere. Howe'er( this tutorial will make little sense outside of Second Life. LSL is 'ery specific to Second Life. )ou will begin by running the standard "hello world" script and e'entually mo'e towards making your own. )ou will need to be familiar the basic principles of Second Life and general building skills before you can make use of e'erything in this tutorial. Let*s mo'e to step +. "What is LSL,"
make progress. !f you*'e built in Second Life( e'erything you can define in the edit window can be defined in a script. $ll interaction you see between objects or between a'atars and objects is 'ia scripts. Learning more about the world and building model is 'ital to some aspects of scripting( thus !*d recommend a good foundation in building as you learn to script.
he script you made runs the instant you hit sa'e. !f you take it into in'entory( it will "suspend" what it was doing but go right back to it when re88ed again. 6!f you are not familiar with "taking" and "re88ing" an object you may need to re'isit your building skills7. 3ach time you re/write your code you*ll want to reset the script. ry reseting the script in the following ways. +. ?ress #eset in the script window. 1. Select the object and go to OOLSC#3S3 S.#!? S != S3L3. !O= $lso try stopping and starting the script from running 'ia checking and unchecking the "running" button( or the OOLSCS3 S.#!? S O =O #"==!=G != S3L3. !O= and then OOLSCS3 S.#!? S O #"==!=G != S3L3. !O=. Once you get comfortable with stopping( starting( and reseting a script( try changing the words "Hello $'atar" and see what else you can make it say.... for goodness sakes keep it ?G. WH) S O? $=> S $# Scripting in Second Life can be a little bit like fi@ing your car.... while going E:mph down the freeway. hus you need ways to stop the programs for they may affect others. Objects can hold more than once script and they will all run at once. his can be used in the following manner. Say you write a script that makes a prim change color e'ery few seconds. )ou also write one to make it follow you. ?ut them both in one object and it will follow you while changing colorsD %or simplicity*s sake( the following e@amples will all be used indi'idually so be sure not to put two or more into the same object.
$ny line starting with two forward slashes is a comment. !t will not run and is used to help you document your code. 00 his is a comment S $ 3S $ "State" in LSL is a section that is running( and waiting for e'ents. Only one state can be acti'e at any one time per script. 3'ery script must ha'e a default state with at least one e'ent in it. 3@cept for the default state( each state is define by the word S $ 3 followed by the name of the state. he contents of the state are enclosed in two curly brackets. default 9 00 contents of state go here < state playing 9 00 this is a state called "playing" < 3F3= S 3'ents are inside of states. By "inside" ! mean it is between the open and closed curly brackets that represent the body of the state. When that state is acti'e( those e'ents wait to be triggered and run the code inside them. We*'e seen "state5entry" which is trigged by the a state being entered( and "touch5start" which is triggered when you( or anyone( touches an object. Lets take a look at the deafult code. 00 .ode start default 9 touch5start6integer total5number7 00 this is an e'ent 9 00 this is the content of the e'ent < 00 end of e'ent < 00 end of state %"=. !O=S %unctions lay inside of e'ents and are either defined by you or built/in. hose built in to LSL all start with two lower case L*s. We*'e seen llSay67 so far. %unctions take "arguments" or 'alues in the parentheses that follow it. !f you ho'er o'er the function in the editor( a popup will show that tell you what the function is e@pecting. !n the case of llSay it e@pects a number and a string. We send it the number 8ero and the string "Hello( $'atarD" separated by commas. he function is "e@pecting" a number and strings and won*t take anything else.
9 state5entry67 00 this is run as soon as the state is entered 9 llSay6:( "turning offD"7; llSet.olor6H:(:(:C( $LL5S!>3S7; 00 sets all sides as dark as possible < touch5start6integer total5number7 9 state default; < < 00 ///////////////end of code //////////////// $ simplification of this would be default 9 00set color to light and( if touched( enter the "off" state. < state off 9 00set color to dark and( if touched( enter the "default" state. < =ote that after "default" all new states begin with the word "state". $lso( while the object has a te@ture( the color will effect the "tint" more than the true color.
waiting for more e'ents to happen. O".H3> B) $= $F$ $# While idle in the default state a touch will trigger the "touch5start" e'ent. !nside of the "touch5start" e'ent is only one command& state off; his is a command to mo'e immediately to a new state named "off". his state is defined after the default state and nearly mirrors the default state e@cept that it turns the prim dark and when touched will put the script back into default mode. hus creating a loop. +. 3nters default state 1. #uns code in "state entry" J. Waits to be touched. K. When touched enters "state off" L. 3nters "state off". L. #uns code in "state entry" 6note in the "off" state*s body7 M. Waits to be touched. N. When touched enters "default" state. Where the whole thing starts o'er.
llSet e@t6"! am on"( H+(+(+C(+.:7; What do the numbers mean, the H+(+(+C we*'e seen before. !t represents the 'alues for red( green( and blue. %or now just know that H+(+(+C( means "white" and H:(:(:C means "black". #eplace the llSay6:("turnign offD"7; with... llSet e@t6"! am of"( H:(:(:C(+.:7; he +.: is the alpha setting. +.: means fully opa2ue( and :.: would be completely transparent 6in'isible7. #) H!S Where would you put a command to say something just before the script lea'es the default state, answer& O O O O O O O O O O O F #ight abo'e the "state off;" command in the "touch5start" e'ent inside of the default state. >on*t confuse it with the "state off" that defines the start of the new stateD