Difference between revisions of "LSL 101/A Gentle Introduction"

From Second Life Wiki
Jump to navigation Jump to search
 
(19 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Category:LSL 101]]
{{NavNextPrev|prev=|next=The Structure of a Script}}
{{LSL Wikibook Index}}
== A Gentle Introduction to Scripting in Second Life ==
== A Gentle Introduction to Scripting in Second Life ==


This section of 'LSL 101: The Wikibook' is written for the SL user who wants to script the things they build or own, but who perhaps has no experience of any kind with programming.  If that's you, welcome! It will also be of use to people who have experience in other programming languages and would like a gentle introduction to LSL.
'''LSL 101: The Wikibook''' is written for the SL user who has ''no experience with computer programming''; or, for users with programming experience who would like a gentle introduction to LSL.
 
This section assumes you have basic building skills, since scripting is just one aspect of building.  You should be able to create and edit a prim, then take it in and out of inventory.  You should be able to do the same with a notecard and know how to add it to a prim's inventory.
 
If all of this sounds like you, welcome!
 
'''Scripts are used to make SL objects ''do'' something.'''  They can detect things that happen like noticing mouse clicks or the approach of an avatar; and, that script can then act in response by moving, changing appearance, greeting an avatar with a message, or displaying a current stock quote, among many other actions like talking, moving or changing color. 
 
'''Scripts are items placed in object [[inventory]]''' (marked with the [[Image:Inv_item_script.png]] icon) to create an effect. This effect has a very large number of things that it can do. How objects look, move, interact with [[avatar]]s, and communicate are all examples of what can be done with scripts.
 
'''Scripts are created through an in-world editor''' similar to a text file editor. Key words that perform specific actions or run when an action is performed are highlighted. The language used to write scripts is [[Linden Scripting Language]] (LSL), and is a simple but powerful programming language that was created for the SL environmentWhen programmers say "simple" they mean to suggest that LSL is ''more limited'' than other programming languages; not that it is easier to learn.  Like any other language, mastering LSL requires patience and practice.
 
Spoken languages have words which make up vocabulary and a particular order of use, called grammar. Written languages add punctuation - commas, full stops (a/k/a periods), brackets, parentheses and so on - which help to clarify how different words and phrases relate to each other. All combined, vocabulary, grammar and punctuation result in the rules of language called ''syntax''.
 
The difference between computer languages and human languages is that with human languages people can generally understand your meaning even if your pronunciation or spelling is not completely correct or your grammar is slightly wrong. Computer languages are not like that. If your spelling, punctuation or structure is even slightly wrong the computer will report an error (for example "syntax error"), do something completely unexpected or fail to do anything with no explanation. This can be ''very'' frustrating for beginners.
 
In LSL the syntax (also called "structure") is very important. It will be discussed shortly along with an introduction to the vocabulary, grammar and punctuation of LSL shortly.
 
==About & How to Use the Series==
This series of tutorials is written especially for the Second Life user that has some building experience, but little or no scripting (or other programming) experience.  It is divided into "lessons" that are intended to be read sequentially.  Most lessons will take the form of an  example script, followed by a discussion of anything that is newly introduced by that script.  If we've written the lessons successfully and you read them sequentially, you should never come to a page with vocabulary or concepts that are unfamiliar but not explained in that lesson.


Since scripting is one aspect of building, we will assume that you, the reader, have rudimentary building skills.  You should, at minimum, be able to create a prim, edit it, take it in and out of your inventory, be able to do the same with a notecard, and know how to put a notecard into a prim's inventory.  That's about it.  Everything else you'll need, we'll try to cover.
On the other hand, we don't necessarily try to explain ''everything'' about a topic in a lesson when it is first introducedIf you're looking for a more detailed explaination of any particular topic, see the section [[LSL 101/Language Topics in Focus|Language Topics in Focus]].


Whenever you want an object in SL to do something, to respond to a mouse click, to greet someone when they come close to it, to change its texture or colour - all the interesting things that objects do, other than just sit there - a script is neededScripts take note of things that happen and are able to carry out actions in response.
Our examples will be full scripts that you can copy into SL and run yourself.  You can make modifications and run those, to verify that your understanding matches what SL actually does.  One consequence of that though, is that in a few cases (especially in the beginning), the structure of LSL forces us to include something in the example that we're not ready to explainWhen this happens, we'll call it to your attention so you're not left wondering whether you've missed something.


Scripts in SL are written in LSL (Linden Scripting Language), a simple but powerful programming language, tied closely to the SL environment(Simple, in this case, is used to mean it is more limited than many other popular programming languages; not necessarily that it is simpler to learn, though it is not very difficult if you are really interested to do so.)
As you read these pages, if you do come to a place where you feel we haven't adequately explained the example, or are making unwarranted assumptions about the target audience's background knowledge, please let us knowOn each Wiki page, there is a tab labeled ''discussion''.  Click on that tab and tell us what you think.  Don't worry if you don't know the Wiki editing conventions; we'll figure out your intent.  If you have general comments that are relevant to more than one lesson, there is a [http://forums.secondlife.com/showthread.php?t=318943 forum discussion] that is a great place to post your thoughts.


When you learned to speak you discovered that you needed to know not only some words (vocabulary) but also the order in which to use them (grammar). It is the same with computer languages: you have to learn the words and you have to learn the order in which to use them.  When you learned to read and write you discovered that there is also punctuation - commas, full stops (periods, if you are American), brackets, etc. - which help to clarify how different words and phrases relate to each otherThe difference between computer languages and human languages is that with human languages people can generally understand your meaning even if your pronunciation or spelling is not completely correct or your grammar is slightly wrong. Computer languages are not like that. If your spelling, punctuation or structure is even slightly wrong the computer will report an error or, at best, not do what you expected it to do.  This can be ''very'' frustrating for beginners.
Finally if you are an experienced scripter who is reading these pages out of curiosity and comes across something you could improve, feel free to jump in and do itThis is, after all, a Wiki.


Preliminaries concluded, lets [[LSL101: Simple Script Skeleton|get started]].
'''Please click [[LSL 101/The Structure of a Script|here to continue the tutorial]].'''

Latest revision as of 03:47, 30 July 2012

↑̲  LSL 101  ̲↑ The Structure of a Script →

A Gentle Introduction to Scripting in Second Life

LSL 101: The Wikibook is written for the SL user who has no experience with computer programming; or, for users with programming experience who would like a gentle introduction to LSL.

This section assumes you have basic building skills, since scripting is just one aspect of building. You should be able to create and edit a prim, then take it in and out of inventory. You should be able to do the same with a notecard and know how to add it to a prim's inventory.

If all of this sounds like you, welcome!

Scripts are used to make SL objects do something. They can detect things that happen like noticing mouse clicks or the approach of an avatar; and, that script can then act in response by moving, changing appearance, greeting an avatar with a message, or displaying a current stock quote, among many other actions like talking, moving or changing color.

Scripts are items placed in object inventory (marked with the Inv item script.png icon) to create an effect. This effect has a very large number of things that it can do. How objects look, move, interact with avatars, and communicate are all examples of what can be done with scripts.

Scripts are created through an in-world editor similar to a text file editor. Key words that perform specific actions or run when an action is performed are highlighted. The language used to write scripts is Linden Scripting Language (LSL), and is a simple but powerful programming language that was created for the SL environment. When programmers say "simple" they mean to suggest that LSL is more limited than other programming languages; not that it is easier to learn. Like any other language, mastering LSL requires patience and practice.

Spoken languages have words which make up vocabulary and a particular order of use, called grammar. Written languages add punctuation - commas, full stops (a/k/a periods), brackets, parentheses and so on - which help to clarify how different words and phrases relate to each other. All combined, vocabulary, grammar and punctuation result in the rules of language called syntax.

The difference between computer languages and human languages is that with human languages people can generally understand your meaning even if your pronunciation or spelling is not completely correct or your grammar is slightly wrong. Computer languages are not like that. If your spelling, punctuation or structure is even slightly wrong the computer will report an error (for example "syntax error"), do something completely unexpected or fail to do anything with no explanation. This can be very frustrating for beginners.

In LSL the syntax (also called "structure") is very important. It will be discussed shortly along with an introduction to the vocabulary, grammar and punctuation of LSL shortly.

About & How to Use the Series

This series of tutorials is written especially for the Second Life user that has some building experience, but little or no scripting (or other programming) experience. It is divided into "lessons" that are intended to be read sequentially. Most lessons will take the form of an example script, followed by a discussion of anything that is newly introduced by that script. If we've written the lessons successfully and you read them sequentially, you should never come to a page with vocabulary or concepts that are unfamiliar but not explained in that lesson.

On the other hand, we don't necessarily try to explain everything about a topic in a lesson when it is first introduced. If you're looking for a more detailed explaination of any particular topic, see the section Language Topics in Focus.

Our examples will be full scripts that you can copy into SL and run yourself. You can make modifications and run those, to verify that your understanding matches what SL actually does. One consequence of that though, is that in a few cases (especially in the beginning), the structure of LSL forces us to include something in the example that we're not ready to explain. When this happens, we'll call it to your attention so you're not left wondering whether you've missed something.

As you read these pages, if you do come to a place where you feel we haven't adequately explained the example, or are making unwarranted assumptions about the target audience's background knowledge, please let us know. On each Wiki page, there is a tab labeled discussion. Click on that tab and tell us what you think. Don't worry if you don't know the Wiki editing conventions; we'll figure out your intent. If you have general comments that are relevant to more than one lesson, there is a forum discussion that is a great place to post your thoughts.

Finally if you are an experienced scripter who is reading these pages out of curiosity and comes across something you could improve, feel free to jump in and do it. This is, after all, a Wiki.

Please click here to continue the tutorial.