Monday 27 April 2015

Second Life - Learn To Script: Chapter 1

Contents

Chapter 1: The Basics

Welcome to my new series, Second Life - Learn To Script. In this series I will be teaching you in basic terms how to script using LSL for Second Life. We will be making real products which will not only get you scripting but will also give you some products to sell. The products will range from low difficulty like Key Finders, to median skill of donation boxes, right up to a pride product of mine and ATM machine which will allow you to transfer funds between avatars, have other request funds from you, paying them upon your approval and keeping a up to date balance sheet. This chapter is going to be all about the basics so we will not be building anything just yet. So lets get started.

What is LSL?

LSL stands for Linden Scripting Language. It is a state and event driven language. States are easily explained like a light, on and off. These are two states of a light bulb. A car may have off, idle and drive. LSL has as many states as you want. The starting state in a script is always called ‘default’. Events are actions that the object is waiting for. This can be when a user touches the object, pays money into the object or says something to the object. There are many other events that can occur within an object but these are the main ones we will be using in this book.
LSL also contains variables. Variables can be seen as information holders. They hold the information or data that you need throughout the script. Don’t worry too much if all this is going over your head. We will be looking into everything a lot deeper and you will gain the understanding you need.

Script Editor

The script editor is the place you will be doing all of your scripting. To open the editor you first need a new script. There are two ways to create a script. The first way is to right click inside you inventory and choose New Script. This will create a script for you and ask you to name it. Let’s name this one MyFirstScript. Now double click on this new script on the editor will open. The second way we will learn a little later.
Figure 1 shows you what the editor looks like.
You can see inside a code is already written. This is the default code with 1 state and 2 events. The state that is used is the one that every script needs, default. The default state is set when the script is first executed. The two events used are state_entry and touch_start. We will look into these two a little later.
The down side to making scripts this way is that we cannot run or test our code. The way I make scripts is this alternative way. First I create an object within second life. This will only work if you have permission to create objects on the land you are working.
There are many places in second life that allow builders to create objects on their land. One of these for which I use a lot is called Curious. You need to be a member of their group but the good news is, this is free. I personally have my own building platform which I have had for some time now.

Creating an object

Step 1: To create an object click on Build in the menu and select Build. You will be presented with the build window. This tool has many powerful things that enable you to create anything you want. All objects within Second Life were created using this tool. I will not be going into how to use this in this book.
Step 2: You will notice that your mouse has turned into a wand. This is indicating that the tool is ready to make the selected object. Click anywhere on the ground and you will see a cube appear. If this did not work check the setting of the land you are using and ensure you have rights to create objects there.
Step 3: Name your object. Click on the general tab and type in a name for your object. Press enter to finish this step.
Now that you have your object we can create our script the alternative way.

Create a Script inside an object

Step 1: Click on the Contents tab in the build window.
Step 2: Click on New Script
You will notice that a script has appeared in the objects contents called New Script. The object also said Hello, Avatar in local chat. We don’t want our script to be called New Script so let’s go ahead and change it.
Step 3: Right click the script and choose Rename.
Step 4: Let’s call this script MyFirstObjectScript
Step 5: Double Click on the Script
You will see once again the Script Editor has been opened. With the same script that was in the last one you opened. This script editor is a little different than the first with an extra button and a couple of check boxes. The only buttons we will be using are Save and Reset.
If you click reset now you will see the object say Hello Avatar again. The reset button forces the script to start again from the last saved point. Save will save all the new code and try to run the code. If your code has errors they will be displayed in the error box below the written code.
The only check box you will need to be using is the Running check box.
The running check box sets whether the script is running or not. If your script doesn’t seem to be working and you cannot figure out why. This may be the reason.

Variables

Variables are a collection of information or data. There are seven types of variables which you can use.
Type
Brief Description
integer Holds a whole number. A whole number is one that does not contain a decimal. Eg: 1, 2, 3 not 1.5, 2.3, 4.7.
float Holds a decimal number. Can also hold whole numbers.
vector Holds three floats in the form of <x, y, z>.
rotation Holds 4 floats in the form of <x,y,z,s>
key Holds a UUID (special string) used to identify things within Second Life.
string Holds a sequence of characters. EG: Raiden, S1ghfy3 or Raiden Faxel
list Holds a list of data types

All variables have 3 parts to them, the type, the name and the value. In code we would declare a variable like the following.


This is setting three variables. I used the names myNumber, myList and myString but you can use whatever name you want to. The code //Sets a variable and the like are comments. Comments are very important in scripting.
We will learn more about variables when the time comes.

Comments

Comments do nothing in the code. They are simply a comment. Scripters use these within their code to understand what is going on. There has been many times before I used comments that I would write a script. Then months down the line an error would pop up and I would have to fix it. I would open the script and have no idea what was going on. If I had used comments I would be able to clearly see what is happening. I recommend you use comments in every piece of code you write.
To write a comment we simply add two slashes in front of our comment. This tells the script that the following is a comment.

//This would be a comment

In the next post we will be making our first product.

 

 

3 comments:

  1. Hi Tom! I just came across your post on the SL Forums; thank you so much for taking the time and effort to put this series together. LSL is something that really fascinates me but I have no experience in, so I'll be watching (and hopefully following along) in earnest. You rock! :)

    ReplyDelete
    Replies
    1. Thanks EverAfterRSL. When I played it took me ages to learn to script. I recently got my Diploma in Software Development and thought it would be great to teach people in the same manner I got taught to program. I hope it helps you out heaps and if you have any questions or are having problems with your scripts then please comment on the relevant chapter and I will help out.

      Delete
  2. Hello Tom!

    I believe it's always a good idea to share knowledge. I also taught LSL inside SL and it did help me to extend and tighten my own coding skills. Although it's hard to get the best way of what to explain first and how, because learning can be a slow process and people might get confused real quick. Especially if they get to much information within a relative short time. Thats why i decided to split my lessons into small chapters (the basics => states => events => functions => variables => more advanced topics on demand). LSL as a procedural and event driven scripting language is rather simple in relation to most other programming languages. But it can be hard to get if it comes to asynchronous tasks, complex flow controls, in-world or network communication or other advanced topics. Especially the limited capabilities of LSL sometimes makes it hard to achieve a specific goal (like persistant data storage, next owner permissions and internal or external communication). Your instructions seems to be accurate and easy to read but you might want to add or change them by time, when more and more FAQs occur. You just might want to add the possibility of having multi-line comments by using /* for begin and */ for comment end like for script headers.

    Kind Regards,
    Rev

    ReplyDelete