Posted on

Tutorial LSL 102 Class Note

Basic Scripting 102 (Beginners with no programming experience)

Welcome again to my basic scripting class.

~============== Introduction ======================

This class is for beginners who don’t have any experience at all in programming.

If you already have scripting experience in any other language, please consider to take one of the intermediate classes instead. You are welcome to stay but it may be boring 😉

We’re hiring teachers here at the Academy of Second Learning, so if you’d like to help us bring great classes to Second Life, please pick up an application from the blue square sign outside.

Today I will tell you what a script is and how you can create and change it.

This will be very basic and I try to explain anything that is important to start your first script.

Anyway, you may find some stuff confusing if you hear it the first time, and you don’t need to understand all the technical details to create your first script.

So please bear with me, if some of it sounds Chinese to you. You will understand it later. If you have questions, please wait until I did all the explanations.

I will stop from time to time and give you a chance to ask questions.

~========= Chat Issues =============

Make sure to open your chat history (you can use CTRL-H to do this). You can catch up with chat lines that you may have missed.

Later on, after the class, you will be able to select all the chat lines with your mouse, or just click into it and press CTRL-A to select all.

You can then copy the chat history into the clipboard by pressing CTRL-C and pasting it later in any text editor of your choice or in a new note card by pressing CTRL-V.

~================ Presenter issues ==============

I hope you can see the presenter screen with the screenshots. If it is blurry, please wait a while until the images are loaded. It should become clear after a minute.

You can speed it up if you zoom in your camera to make it full screen. You can also right-click on it and choose “Edit”.

Even if you are not allowed to edit the object, it will put the image first into the loading-queue, which means, the presenter image will load next on your computer.

~=============================================

We will create our own script today from scratch. Anyway you will get class notes after class to review all the details.

For now it’s important that you just get a hands-on experience with your first script.

If you have to choose between understanding and a working script, choose the working script and re-read the class notes later, that’s much more fun 😉

Anyway feel free to stop me if I’m going too fast for you to read all the stuff 😉

~===================== Default Script basics =========================

So let’s look how we can create a script.

To do that, please REZ a PRIM (any prim will do), I will do a BOX here.

It’s always a good idea to rename everything after creation.

So go to the GENERAL tab and name your object “My scripted Object”

Now, change to the CONTENT tab and click on NEW SCRIPT

There will be now a new script named NEW SCRIPT

You can rename it to a more meaningful name.

RIGHT-CLICK on the name and choose RENAME

Name it now “My first Script”

Now open the script by DOUBLE-CLICKing it

You can also RIGHT-CLICK and choose OPEN

What do you see?

It is an editor window, quite similar to the editor where you write note cards.

The differences are:

1. There is a special Menu available

2. There is some kind of status information at the bottom of the window

3. You will see the text in different colors.

I will tell you more about the editor in the next class, when we start to make changes in the script ourselves.

For now, I will just discuss the structure of the script as you see it here.

The very important part for Beginners:

Don’t be scared by the structure that you see in this script. It’s called the “Default Script” that will be created every time when you create a new script.

You can see it like a skeleton. There is no need to change that skeleton or add anything to it. We will just add some meat to it now.

You will be able to understand the general structure later, for now we will just change the lines that we have already in the default script.

Anyway, I will explain what you see here, to make it easier to understand later, if you continue your scripting experience.

First, there is the word “default”

That means, it’s the start of the DEFAULT state for this script or object that is attached to the script

Every script has a DEFAULT STATE.

You will recognize that some lines are surrounded by curly brackets. These curly brackets build a CODE BLOCK.

That means all lines between a { and } belong together. You can imagine it as a paragraph in a book.

Code blocks are useful if you want to group more than one line of code (you may also say: more than one sentence in one paragraph).

There are some exceptions where you don’t need these code blocks, but it won’t hurt to use them anyway. So for beginners, it’s always a good idea to use them as shown in the default script.

For each opening curly brace { there must be a closing one.

To make it much more complicated, these Block can be NESTED, that means you can have such a CODE BLOCK inside another CODE BLOCK.

To make it more readable, these blocks are indented to show what belongs together.

So for Beginners: Just don’t touch the curly brackets unless you really know what you are doing 😉

All the lines that are between the first { and the last } will belong to the DEFAULT STATE that we are going to ignore for this class 😉

~===================== The Event ======================

The first line inside this default state reads:

state_entry()

This is an EVENT.

Not to confuse with a party or education event in SL 😉

The EVENT concept maybe hard to understand for somebody who has not programmed before, so let’s try to say it in simple words:

Just Imagine it’s something that just HAPPENS without the need to TELL the Object to do anything specific.

For example: state_entry will happen every time when the script is started or if you change and save your script for that object. You can’t stop it, and you can’t repeat it at your will.

It just happens, like your object has a life of its own.

And what happens, when you create your script? It tells you “Hello, Avatar” in the Main Chat. That’s a nasty thing in classes, because it will spam all over the chat with these “Hello, Avatar” lines 😉

To prevent all this spam, add the characters // at the beginning of the line:

for Example: // llSay(0, “Hello, Avatar!”);

The // will tell the script to ignore the rest of the line.

This is useful if you want to disable a line in a script, but you don’t want to delete it entirely.

It will also be used for comments in a script. For example it you want to document what you are doing in the script.

Theses comments are useful for you, if you want to review and change your own scripts later, or for other people who want to use or change your script.

You often find such comments

like: // enter your e-mail address in the next line

Or: // Copyright by Jim Gustafson

or: // this is a simple vendor script, contact Jim Gustafson if you need help

When you have disabled the llSay-Line, and you save your script now (just click on the save button), we should no longer have these “Hello, Avatar” in the chat.

Everybody could manage that?

~=============== Functions ================

Now, let’s go on with the default script

We will find another event in the script: touch_start

This event happens every time, when the object is touched. You will find this line in the code block for touch_start:

llSay(0, “Touched”);

llSay() is a FUNCTION in LSL

BTW: LSL is short for Linden Scripting Language 😉

A function is something that you can CALL in LSL

It’s like Ali Baba calling “Open Sesame” to open the door 😉

It is like giving your Object a command like in your life if you say “come here!” or “go away!”, or “buy me a drink!”;

Sometimes, you have to PASS one or more PARAMETER to this FUNCTION

They are in the round brackets and will be separated by commas

Parameters are like detailed instructions for a function. In real life you would say “Buy me a drink! I like a red wine”.

If you would say it like a computer you would say:

buy_me_drink(“wine”, “red”);

buy_me_drink is the FUNCTION here,

“wine” and “red” are two PARAMETERS for this function.

You may have noticed, that every function call is ended by a semicolon ;

That’s not part of a smiley – it is part of the SYNTAX

SYNTAX is the GRAMMAR of your script. If you do it wrong, your object won’t understand you 😉

In English, if you use wrong grammar, the other person may understand you anyway.

But in Computer-Language, the computer has no mercy.

If the SYNTAX is wrong, the computer WON’T WORK.

But no panic!

The computer will tell you, if something goes wrong.

Just try it out.

Remove the semicolon at the end of the line that says llSay(0, “Touched”), then click SAVE.

You will get a message in the editor:

(10, 4) : ERROR : Syntax error

That means:

In line 10 column 4 there is a problem.

Anybody have the same or a similar result?

~================ explain error messages ================

The computer tries to read ahead, because it expects a semicolon at the end of the command line.

If it is not found in line 9, it will commence to line 10

And there, it will find the closing curly brace } that indicates the END OF the CODE BLOCK

So, sometimes, error messages will not exactly point you to the faulty line …

… but maybe to the next line in the script.

If you add the semicolon again …

… you can save the script without error.

Succeeded everybody?

~==================== explain llSay =====================

As we have seen, the function llSay() is used to print a text in the main chat.

The first parameter is the number 0

That means the output goes to channel 0 which is the main chat window.

The second parameter is a string.

That is the text that is printed into the chat.

In a script you can have different types of data that can be passed as function parameters.

For example 0 is an integer; integer is just another name for a number

A string is a sequence of letters that can be displayed in a chat for example

You can look it up in the Wiki:

http://rpgstats.com/wiki/index.php?title=LlSay

A Wiki is a special Website, where knowledgeable people can build some kind of documentation that can be changed easily.

If you have a chance to open the URL in a browser, you can do this now. If not, you can explore the LSL-Wiki later.

The LSL-Wiki is the best reference for scripting at the moment.

It’s not complete, that means some explanations are still missing. But it will be completed over time.

You may also notice, that every Function starts with ll which is short for Linden-Language and must be two lower case letter L

For llSay you will find a so called “PROTOTYPE” of the function in the Wiki:

llSay(integer channel, string text)

That means, llSay is called with two parameters (here named channel and text)

The first one must be an integer

The second one must be a string

If you read further in the Wiki, you will find:

“Says text on the specified chat channel. The range is a 20m radius sphere.”

This is the explanation of the function. The names of the prototype parameter are used here to explain the usage of the function.

The next line in the wiki is a note:

“Note: A long string for text will be truncated to 255 bytes.”

For other functions you may see a lot more explanation and more notes, but llSay is a very easy and basic function.

Any questions so far?

~================== changing llSay to llOwnerSay in touch_start ========

To avoid text spam from the object here in class, change the line with llSay to another function. The new Function is llOwnerSay and you have to change the line like this:

llOwnerSay(“Touched”);

Make sure you delete the first parameter (the number 0 and the comma) from the parameter list.

Then hit “save” and wait a moment. Saving the script may need a few seconds. It will say “Save complete” if there was no error in the script.

All okay so far?

llOwnerSay has the same effect as llSay, but the text will only show up in the chat window of the object’s owner.

So everybody can only hear their own object.

You can do other fancy things in the touch_start event. We will come back to that later in the next class “basic scripting 103”.

~========== The editor ==============

For now, we will just explore the script editor.

You see the different colors. Events and Functions do have their own colors.

This helps to avoid typos. If you write something wrong, it will not have the correct color.

For example: States and functions are red.

Events are blue

Comments (beginning with //) are orange

So you can easily see that something is wrong.

The editor will also help you with code blocks.

Delete the whole code block in state_entry, just select all lines from the opening curly bracket { after state_entry until the closing brackets } and hit delete.

Now enter a new opening curly bracket after state_entry()

To do that, place your cursor directly after state_entry() and press the RETURN key

The editor will INDENT your cursor automatically under state_entry()

Now type the curly brace { and hit RETURN again

You will see, the cursor is INDENTED again. There you will be able to write all your commands that belong to this code block. In this case type:

llOwnerSay(“Hello”);

(don’t forget to end the line with a semicolon 😉

Press RETURN again. The cursor will be placed indented directly under your line.

Now enter the closing curly brace }

You see, that the cursor will change the indentation and jump back to the left.

This method does not always work properly. For example if you indent your line manually by pressing the TAB-Key (Tabulator key), it can be messed up.

For that reason, it’s always a good idea to count your opening and closing brackets afterwards 😉

What I do usually: I close every bracket immediately and then fill in a new line between them. Let’s try that:

Delete the code block again.

Place the cursor after state_enty() and press RETURN

The cursor is now indented under state_entry, now type the opening brace { and press RETURN

Now type the closing brace }

Your code block is now lined up correctly but you don’t have anything between the brackets.

Now GO UP one line, place the cursor past the opening brace {

… and press RETURN.

Now you can enter your line:

llOwnerSay(“Hello”);

This seems to be more complicated, but will prevent that you forget to close brackets.

~========== Conclusion ==============

This concludes this class.

Please take the class notes from the big box.

Now you will be able to create your own simple script.

You are able to use the script editor

You are able find simple syntax errors in the script and fix them.

At last, maybe you can do me a favor:

These classes are free of charge, because ASL has sponsors.

These sponsors would like to know how the classes are accepted by the students.

So please fill out the feedback form that I gave you with the class notes and drop it back in the ASL Survey Box (the blue scroll near the landing point)

Thanks for your time 😉

Advertisements

About Kemo

I am just an ordinary person in the garden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s