[go: up one dir, main page]

0% found this document useful (0 votes)
170 views8 pages

Getting Started in LSL Scripting in Second Life

This document provides an introduction to scripting with Linden Scripting Language (LSL) in Second Life. It explains that LSL scripts allow objects to have interactive behaviors and communicate with each other. The tutorial walks through running a basic "Hello World" script, which prints a message when the scripted object is touched. It emphasizes the importance of states, events, and functions in LSL scripts. The document also provides tips for debugging scripts, such as resetting scripts when errors occur.

Uploaded by

Scott Giefer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views8 pages

Getting Started in LSL Scripting in Second Life

This document provides an introduction to scripting with Linden Scripting Language (LSL) in Second Life. It explains that LSL scripts allow objects to have interactive behaviors and communicate with each other. The tutorial walks through running a basic "Hello World" script, which prints a message when the scripted object is touched. It emphasizes the importance of states, events, and functions in LSL scripts. The document also provides tips for debugging scripts, such as resetting scripts when errors occur.

Uploaded by

Scott Giefer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

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,"

step 1What is LSL?


LSL is the Linden Scripting Language. his is the language all scripts in Second L!fe are written in. !ts structure is based on -a'a and .. 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 a'atar( but not inside an a'atar. hey are written with a built/in editor0compiler which we will access in step 1. One thing that makes LSL uni2ue is it*s hea'y emphasis on "States" and "3'ents". $ door can be "open" or "closed" and a light can be "on" or "off". $ person can be "hyper"( "calm"( or "bored". 4any real life objects ha'e "states" and the same can be true for LSL programs. 4inimally a script will ha'e one state( the default state. $n e'ent can be thought of as a " rigger". 3'ents are not user defined in Second Life but rather predefined in LSL. One called ouch5start67( will trigger the code in it when the object running the script is touched. So the minimum LSL program must ha'e one state with one e'ent in it. Here is a look at a minimal program in written in LSL that can loosely be translated as...."When ! am in the default state( and ! am touched( say "Hello World" on channel 8ero". default 9 touch5start6integer total5number7 9 llSay6:("Hello World"7; < < WH$ .$= ! >O W! H S.#!? S, Scripts can make an object mo'e( listen( talk( operate as a 'ehicle or weapon( change colour( si8e or shape. $ script can make an object listen to your words as well as talk back to you( scripts e'en let objects talk to each other. he most basic object in Second Life is the "?rim" or primiti'e( the basic building block of all objects you can build in Second Life. When se'eral prims are linked( they can each contain a script which speaks to the rest of the object 'ia Link 4essages. hese are faster and more pri'ate than ha'ing objects "chat" or email each other. hese are beyond the scope of this tutorial and we will instead focus on single scripts in a single prim. Scripting is harder to learn than basic object manipulation( but is 'ery rewarding once you

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.

step 2Running Your First Script


raditionally one starts by writing the smallest program possible to print "hello world". Since LSL only runs inside objects( you must know how to create an object and put a script inside it. )ou must be on land which allows building. 3ither your own land( or land where you ha'e permission to build on such as a sandbo@. #ight click on the ground and choose "create". 6for one button macs use commandAclick7 By default( you should see a "wand" icon with which you can click and create a cube on the ground. )ou will automatically enter "edit" mode and an edit window will pop up. 6=ote& to place a script in an e@isting object( right click it and hit edit to open the edit window.7 !n the edit window you may see a button marked "moreCCC" click it to re'eal fi'e tabs marked general( object( features( content( and te@ture. .lick "content". his window shows the contents of an object which can hold scripts( notecards( e'en other objects. ?ress "new script" to add a new script. his will open the LSL editor with a default script. his editor will color code your synta@ and pro'ide some info about keywords when you hold your mouse o'er them. !t will also do basic synta@ checking. Before e@plaining the code( lets run it. Hit "sa'e" and close your edit window 6not the LSL editor window7. )ou should see the words "Hello $'atar" from "object" !f you touch the object( it will say " ouched." 6make sure the "edit" building window is closed for touching to work. .ongratulationsD )ou ha'e compiled and run your first LSL scriptD

step 3Wash / Rinse / Repeat


We now ha'e a running script( howe'er most scripts you make won*t run the first time you run them. !t will take many tries as you correct errors and make impro'ements. When you hit "sa'e" on a script( the LSL editor "compiles" the code to something LSL can understand. !t will stop howe'er if it finds an error. Brackets( parenthesis( and semicolons must all be perfectly in place before a script will run. !f you are new to programing this can be one of the most infuriating steps and lead you to screaming >W!4 6>o what ! meanD7 ?art of becoming a programing in $=) language is learning how to precisely define steps and correctly type them into the language you are working in. hus you will find yourself writing( running( then #3/writing your code se'eral times.

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.

step 4A C oser Loo!


Lets take a look at the deafult code. 00 .ode start default 9 state5entry67 9 llSay6:( "Hello( $'atarD"7; < touch5start6integer total5number7 9 llSay6:( " ouched."7; < < 00 .ode end he code abo'e contains 1 comments( + state( 1 e'ents and 1 functions. Lets look at them indi'idualy. .O443= S

$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.

step "#utting it a together


Line by line( here is the hello a'atar script. .opy and paste this to a new script to see it color coded. 00 $ll Scripts need a >efault State default 00 this open curly bracket denotes the start of the state 9 state5entry67 00 an e'ent 00 another curly bracket starts the body of the e'ent 9 llSay6:( "Hello( $'atarD"7; 00 a function inside the e'ent < 00 closed curly bracked closes the state5entry e'ent touch5start6integer total5number7 00 another e'ent inside default state 9 llSay6:( " ouched."7; 00 a function between the brackets of the touch5start body < 00 end of touch start < 00 .ode end he instant you sa'e your script( it enters default state( which in turns runs the "state5entry" e'ent which in turn runs the funciton llSay67 which makes the object talk. $fter this the program waits idle in the default state until a new e'ent is called. ouching the bo@ triggers the e'en "touch5start" which also makes the object speak.

step $%ntroducing States and &'ents


LSL scripts will not run beginning to end . !nstead they will look for a default state and wait for an e'ent. Within those e'ents( there can be a call to go to a new state. Lets look at a script with two states with two e'ents in each. he %ull .ode& GGGGGGGGGGGGGGGGGGGGGG default 00default state is manditory 9 state5entry67 00 runs each time time state is entered 9 llSay6:( "turning onD"7; 00object speaksD llSet.olor6H+(+(+C( $LL5S!>3S7; 00 sets all sides to most bright 00 note the semicolons at the end of each instruction. < touch5start6integer total5number7 00 another e'ent with only one function inside 9 state off; 00 sets the script to a new "state" an starts running "state off" < < 00 this curly bracket ends the body of the default state. state off 00 a second state besides "default"

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.

step (A c oser oo!


Let*s e@amine the default state. %irst we see the "state5entry" e'ent( which gets triggered each time the default state is entered. S?3$I O 43D he first line in the e'ent state5entry is... llSay6:( "turning onD"7; his makes the object speak "turning onD" on channel 8ero. What is channel 8ero, !t is the same channel you see all public chat on. $ semicolon ends the line and yet another instruction follows. llSet.olor6H+(+(+C( $LL5S!>3S7; his turns the prim to it*s brightest tint. !f you take the te@ture off the prim( you*d see it as bright white( with a te@ture( it looks "normal". he three +*s stand for the #ed( Green( and Blue( 'alues of the tint. $fter that the e'ent is done with the two lines of commands( the script waits idle in default

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.

step )A fina *ord on *ords


Objects speaking is a great way to know what a script is doing( but e'eryone can hear it for J:m all around you. $s you get into more comple@ scripts this can get pretty noisyD hree alternati'e ways to see what is going on e@ist. SHHHH WH!S?3# llWhisper6 7 is just like llSay6 7 but only broadcasts at half the distance. )ou still l must state what channel. So.... llWhisper6:("turnign onD"7; ...might work a bit to sa'e the sanity of your neighbors. "sing llShout6 7 doubles the distance heard( but can cut the amount of friends you ha'e in half. &/> llOwnerSay6 7 uses no channel and is heard only by you. Fery usefull and can triple the amount of friends you ha'eD llOwnerSay6"turnign onD"7; H3 SO"=> O% S!L3=.3 )ou can make a totally silent message 'ia llSet e@t6 7 like this.

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

You might also like