Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

New Scripters Start Here (FAQ)

Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
07-18-2006 10:21
Welcome to scripting in SecondLife.

The scripting language in SecondLife is called LSL (Linden Scripting Language) and it is like no other language. The LSL syntax is almost identical to that of C & C++ but that is where the similarities with other languages end.

Wiki:
The best resource for scripting knowledge (besides an expert scripter) is the LSL Wiki
http://lslwiki.net/lslwiki/ (mirror*) followed by the LSL Portal https://wiki.secondlife.com/wiki/LSL_Portal** Both are user maintained resources that are more then just a manual but a living repository of script examples and know-how.

Examples, Libraries & Other Publicly Available Scripts:
There are alot of free scripts floating around. Writing and debugging scripts is time consuming. A great way to avoid this is to search the existing script Libraries:
LSLWiki Script Libary (mirror*)
LSL Portal Script Libary
Script Library Forum - Do not post anything but completed scripts to this forum.
LSL Repository
There are also a number of script libraries inworld.

Tutorials:
A number of tutorials have been written to help new scripters learn LSL. These have been posted on the LSL wiki:
http://lslwiki.net/lslwiki/wakka.php?wakka=LSLTutorials (mirror*)
https://wiki.secondlife.com/wiki/Category:LSL_Tutorials

Classes:
In SL on an almost daily basis free scripting classes are held. The vast majority of scripting classes are free, and you don't have to sell your soul to attend them (it really is, a free lunch). The classes can be found by searching the event calender:
http://secondlife.com/events/index.php?category=26
Or you can search inworld as well.
Most scripting classes also have a script library on sight with example scripts.

Forums:
Of course another great resource are these forums; the LSL community can be leveraged to answer questions though these forums.

Advice & Tips:
If your code isn't working, post a section of the code that isn't working or a demo script that doesn't work. It's really hard to debug something when it is described; the source really really helps us help you.

The wiki is the best resource for finding answers, if you have an implementation problem, you will want to familiarize yourself with the functions that could apply; hence the categories of functions on the LSL Wiki:HomePage & LSL Portal:LSL Portal

Both the wiki and these forums have a search; please use them, you may find your questions have already been answered.


Please do not post comments to this thread, but please add additional resources and links.

Mirrors:
  1. LSL Wiki Mirror
  2. LSL Wiki Mirror / Redirect / Portal Mirror
  3. LSL Wiki Mirror 01-05-2007
  4. LSL Wiki Mirror 01-05-2007 Alternative


*rpgstats isn't really a mirror of the LSLWiki but an undertaking to move over to the MediaWiki engine (the old wiki is using wakka). The content is going to be out of sync between the two wikis.

** The LSL Portal is on the Official Secondlife Wiki. For legal reasons content cannot be directly copied from the old wiki to the official wiki. It like rpgstats uses MediaWiki. The effect has been to leave the community with three different LSL wiki's they can contribute to.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
07-24-2006 20:27
When posting scripts on the forums, use the PHP tags. These tags cause scripts to keep indentation and have syntax highlighting.
You type:
[PHP]
//This is a comment
[/PHP]

The board displays:
CODE
//This is a comment

When posting a notecard or related text, use the CODE tags. These tags will keep indentation and use a monospaced font.
You type:
[CODE]
Notecard line 0.
[/CODE]

The board displays:
CODE
Notecard line 0.
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
No, you can't write to notecards
08-16-2006 03:13
lsl can NOT and almost certainly never will be able to write to notecards, or create them.

Kelly Linden has explained this - basically creating a new notecard creates a new asset. LL are rather sensibly wary of such things since the asset cluster (in any of its various ruder names) quite often struggles under the much lower load that its current use generates.

http://secondlife.com/badgeo/wakka.php?wakka=hacks lists some ways you can store small amounts of data in world part way down. Otherwise it's farm it out to a server somewhere with llHTTPRequest or similar.
_____________________
Eloise's MiniMall
Visit Eloise's Minimall
New, smaller footprint, same great materials.

Check out the new blog
Dimentox Travanti
DCS Coder
Join date: 10 Sep 2006
Posts: 228
09-28-2006 10:30
The LSL Repository also is at..

http://lsl.dimentox.com

Thanks,
Dim.
_____________________
LSL Scripting Database - http://lsl.dimentox.com
Radar Masukami
Registered User
Join date: 30 Sep 2006
Posts: 25
11-23-2006 16:20
From: Eloise Pasteur
lsl can NOT and almost certainly never will be able to write to notecards, or create them.



(snip)

From: someone

http://secondlife.com/badgeo/wakka.php?wakka=hacks lists some ways you can store small amounts of data in world part way down. Otherwise it's farm it out to a server somewhere with llHTTPRequest or similar.


It just seems to me that the lack of real data storage is hugely limiting the variety of apps and really interfacing the web with SL. I personally can't believe it wasn't the first thing addressed when building LSL, it's so basic and required on the web these days.

Huge shortcoming. It requires people to use their own server resources at additional expense to themselves.
Greenman Mondegreen
Registered User
Join date: 23 Jan 2007
Posts: 0
T A N S T A A F L !!
01-23-2007 07:48
There Ain't No Such Thing As A Free Lunch!

Deal with it.
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
If Else If
03-07-2007 05:24
If you are checking a set of mutually exclusive results use else if's

Consider the following code fragment:-
CODE

list MENU = [ "A" , "0" , "1" , "2" , "3" , "4" , "5" ];


listen(integer channel, string name, key id, string message)
{
if (llListFindList(MENU, [message]) != -1) // verify dialog choice
{
if (message == "A")
ShowMenuA(); // present submenu on request
if (message == "0")
ShowOptionMenu0(); // present submenu on request
if (message == "1")
ShowOptionMenu1(); // present submenu on request
if (message == "2")
ShowOptionMenu2(); // present submenu on request
if (message == "3")
ShowOptionMenu3(); // present submenu on request
if (message == "4")
ShowOptionMenu4(); // present submenu on request
if (message == "5")
ShowOptionMenu5(); // present submenu on request
}
}


Even if message does indeed equal "A", the 5 subsequent IF statements will still be checked. A more efficient form would be :-

CODE

list MENU = [ "A" , "0" , "1" , "2" , "3" , "4" , "5" ];

listen(integer channel, string name, key id, string message)
{
if (llListFindList(MENU, [message]) != -1) // verify dialog choice
{
if ("A" == message)
ShowMenuA(); // present submenu on request
else if ("0" == message)
ShowOptionMenu0(); // present submenu on request
else if ("1" == message)
ShowOptionMenu1(); // present submenu on request
else if ("2" == message)
ShowOptionMenu2(); // present submenu on request
else if ("3" == message)
ShowOptionMenu3(); // present submenu on request
else if ("4" == message)
ShowOptionMenu4(); // present submenu on request
else if ("5" == message)
ShowOptionMenu5(); // present submenu on request
}
}

Now the first match will inhibit all the subsequent tests.

This still isnt the most efficient solution however.
String compares are far less efficient that numeric compares.

CODE


list MENU = [ "A" , "0" , "1" , "2" , "3" , "4" , "5" ];

listen(integer channel, string name, key id, string message)
{
integer index = llListFindList(MENU, [message]);
if(index != -1) // verify dialog choice
{
if(0 == index)
ShowMenuA(); // present submenu on request
else if (1 == index)
ShowOptionMenu0(); // present submenu on request
else if (2 == index)
ShowOptionMenu1(); // present submenu on request
else if (3 == index)
ShowOptionMenu2(); // present submenu on request
else if (4 == index)
ShowOptionMenu3(); // present submenu on request
else if (5 == index)
ShowOptionMenu4(); // present submenu on request
else if (6 == index)
ShowOptionMenu5(); // present submenu on request
}
}



Why's Newgy got it all back to front?
If you're wondering why I have have what seems to be an illogical syntax for my if statements its because it uses the LSL compiler to check that I've got my brain in gear.

If I goof up and write
CODE
 if( index = 6)


The LSL compiler is quite happy and will assign index a value of 6, where as if I'd written

CODE
 if( 6 = index)


The compiler will complain.
ed44 Gupte
Explorer (Retired)
Join date: 7 Oct 2005
Posts: 638
05-22-2007 04:37
This will not pass Programming 101, but it overcomes some of the stack overflow problems with our lsl compiler (before it is even run!). Hopefully mono will let us revert to Newgate's way to get the most out of each 16 K Byte script space.

CODE

list MENU = [ "A" , "0" , "1" , "2" , "3" , "4" , "5" ];

listen(integer channel, string name, key id, string message)
{
integer index = llListFindList(MENU, [message]);
if(index != -1) // verify dialog choice
{
if (0 == index) { ShowMenuA(); return; }
if (1 == index) { ShowOptionMenu0(); return; }
if (2 == index) { ShowOptionMenu1(); return; }
if (3 == index) { ShowOptionMenu2(); return; }
if (4 == index) { ShowOptionMenu3(); return; }
if (5 == index) { ShowOptionMenu4(); return; }
if (6 == index) { ShowOptionMenu5(); return; }
}
llOwnerSay ("Could not decode " + (string) message);
}


The matrix of if statements and very simple sub calls also would not pass Programming 101 but it makes it easier to spot mistakes in the copying and pasting most programmers use to produce this kind of code.
raidingyour Chambers
Registered User
Join date: 12 Oct 2006
Posts: 2
Efficient Switch
07-11-2007 13:29
Wouldnt using a Switch be more efficient again?

Switches are a lot less demanding on a server than if else statements.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
07-11-2007 15:05
From: raidingyour Chambers
Wouldn't using a Switch be more efficient again?

Switches are a lot less demanding on a server than if else statements.


LSL does not have a switch statement.
See: https://jira.secondlife.com/browse/VWR-1287
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
07-14-2007 10:23
From: ed44 Gupte
This will not pass Programming 101, but it overcomes some of the stack overflow problems with our lsl compiler (before it is even run!). Hopefully mono will let us revert to Newgate's way to get the most out of each 16 K Byte script space.

CODE

list MENU = [ "A" , "0" , "1" , "2" , "3" , "4" , "5" ];

listen(integer channel, string name, key id, string message)
{
integer index = llListFindList(MENU, [message]);
if(index != -1) // verify dialog choice
{
if (0 == index) { ShowMenuA(); return; }
if (1 == index) { ShowOptionMenu0(); return; }
if (2 == index) { ShowOptionMenu1(); return; }
if (3 == index) { ShowOptionMenu2(); return; }
if (4 == index) { ShowOptionMenu3(); return; }
if (5 == index) { ShowOptionMenu4(); return; }
if (6 == index) { ShowOptionMenu5(); return; }
}
llOwnerSay ("Could not decode " + (string) message);
}


The matrix of if statements and very simple sub calls also would not pass Programming 101 but it makes it easier to spot mistakes in the copying and pasting most programmers use to produce this kind of code.

It passes Programming 101 just fine, with your point about spotting mistakes being one of the reasons it passes.

Perhaps you've been misled into thinking that the one-exit-point rule is universally accepted. It's not, it's controversial, and this is a good example of where it's appropriate, and possibly even faster and smaller than Newgate's approach.
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
07-15-2007 07:24
From: Kidd Krasner
It passes Programming 101 just fine, with your point about spotting mistakes being one of the reasons it passes.

Perhaps you've been misled into thinking that the one-exit-point rule is universally accepted. It's not, it's controversial, and this is a good example of where it's appropriate, and possibly even faster and smaller than Newgate's approach.


My adherence to single entry single exit design is ingrained through many many years of coding standards that dictated it. Its clean and simple to follow but I'm the first to agree the individual returns should be faster. But from my understanding of the LSL compiler and the produced byte codes, which is admittedly limited, I dont think it will be smaller.
_____________________
I'm back......
ed44 Gupte
Explorer (Retired)
Join date: 7 Oct 2005
Posts: 638
07-16-2007 02:48
Maybe we can get some enthusiastic open source client programmers on SLDEV to improve the compiler so these hacks are not necessary.

I think Babbage mentioned somewhere that with Mono some of the compiling will take place on the server.

Having worked in industry for many years, and now working in an academic environment, I find industry software managers much stricter in following coding standards than my academic colleagues.

edit: my hacks were not to make it faster, but to overcome stack limits in the compiler. There is plenty of memory left when I actually get to run the code.
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
07-17-2007 08:53
From: Newgate Ludd
My adherence to single entry single exit design is ingrained through many many years of coding standards that dictated it. Its clean and simple to follow but I'm the first to agree the individual returns should be faster. But from my understanding of the LSL compiler and the produced byte codes, which is admittedly limited, I dont think it will be smaller.

I've never looked at LSL byte code, but my reasoning is that an else branch needs a target location to which to jump, while a return statement typically doesn't. But that's more based on my experience with real hardware instead of virtual machine byte codes, it assumes variable length byte codes, and it wouldn't apply to calling models that require a called function to have explicit cleanup code before returning.

As for enforced coding standards, managers should stand behind their technical teams' standards, but shouldn't be involved in defining them. I've been on some very good teams with very good standards, but this particular point has never been included in such standards.
Ego Essex
Registered User
Join date: 8 Oct 2006
Posts: 9
Starting
07-20-2007 15:06
I can understand the coding a little bit, but I am brand brand brand new, and I just want to know how you guys figure out the order of everything? and what the begining "integer=
and things lik ethat means..and ..... Why do some scripts not have them? It's okay if you don't answer because it's a really stupid question, but I'd like to know.
Ed Gobo
ed44's alt
Join date: 20 Jun 2006
Posts: 220
08-10-2007 07:23
From: Ego Essex
I can understand the coding a little bit, but I am brand brand brand new, and I just want to know how you guys figure out the order of everything? and what the begining "integer=
and things lik ethat means..and ..... Why do some scripts not have them? It's okay if you don't answer because it's a really stupid question, but I'd like to know.

Not at all a stupid question, though maybe in the wrong place as this is a sticky.

A simple explanation:

A program consists of data and code. In LSL it pays to look at the code first. You start by looking at the event entry points, usually near the end of the script, starting with on_rez and moving onto state_entry. After that you look at the listen codes, both chat and linked. In turn they might call some other stuff before the "default" line.

This code needs to remember stuff from line to line, so we use global variable before the "default" state that can be accessed from anywhere in the script, and local variables that can only be accessed from the function in which they are defined. Throw in a few loops and conditional tests on these variables, and your code can get quite complex.
Kellan Dagger
Registered User
Join date: 23 Jun 2007
Posts: 1
....
02-05-2008 18:58
crap, i only know torquescript :P
HarleyMC Homewood
Registered User
Join date: 31 Mar 2007
Posts: 15
broken urls
04-24-2008 20:47
too many broken urls for this to be a useful starting point
Tek Bachman
Registered User
Join date: 1 Sep 2005
Posts: 5
trap script
05-23-2008 06:13
anyone know where i can get a full perm trap script? or create one?
Cloud Renierd
Registered User
Join date: 6 Feb 2009
Posts: 6
03-20-2009 08:02
From: HarleyMC Homewood
too many broken urls for this to be a useful starting point


Agreed, this could use an update to remove the broken URLs. Although, the most useful links do work still. That's always good.
Kiralily Mubble
Registered User
Join date: 16 Jun 2008
Posts: 1
im new to scriptin n animationd help needed please
06-23-2009 14:01
hi all im startin to make animations through a program i hav made a couple one male female kiss type thing but put them into sl n having trouble getting them to sync they was perfect in the program outa sl i was using i went n bought a syncin script but i couldnt view script n it was only modifiable through notecard n its been a dam pain any1 help please !!!!!!!! contact me through sl if pos kiralily mubble many thanks it shall be greatfully appreciated
Sookie Perlmann
Registered User
Join date: 13 May 2008
Posts: 2
point me in the direction plz?
12-27-2009 21:08
Can someone plz point me in the direction of some scripting classes? I have a project Im trying to create and I have no idea where to begin.....Im trying to create an object with menu options that is interactive, however I do not want it to dispense objects or rezz balls etc.....any ideas?
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
12-27-2009 21:22
From: Sookie Perlmann
Can someone plz point me in the direction of some scripting classes? I have a project Im trying to create and I have no idea where to begin.....Im trying to create an object with menu options that is interactive, however I do not want it to dispense objects or rezz balls etc.....any ideas?

Spend some time with the tutorials at http://wiki.secondlife.com/wiki/LSL_Tutorial and then start playing with sample scripts, either in the wiki or freebie ones that you find in world, to see how they tick. That's probably the fastest way to learn.
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at