Tuesday, 5 May 2015

Secondlife - Learn to Script: Chapter 2

Contents

Chapter 2: Let’s Make a Key Finder

States

The first thing in scripting is writing your states. Like I said earlier these can be seen like a light bulb. A light bulb has two states, On and Off. The default state being off and another state being on. A script can have many different states which you can name anything you want. A state name must not have spaces. The default state is called default. This one MUST be called default and must be in the script. A state is written like this

This snippet of code has two states in it, default and nextState. All states must have the keyword state in front of it to tell the script that it is a state. The only one that is allowed to break this rule is the default state. This is because default is also a keyword that the code recognises and knows it is the default state. Notice also how both have curly brackets { }. These brackets indicate the start and end of the state. The code you write into each state is placed in the middle of these brackets. When this code starts it will be in the default state. It will not enter the nextState until told to do so. This can be done using the code state nextState;. 

A demonstration on States

Step 1: Open the same script we created earlier, by opening the script editor in the object. To find it rezz out the object which had the script inside. Right click the object and choose Edit. This will then open the build window. Click content and double click the script.
Step 1: Delete all the code that is already in the script we have just created.
Step 2: Copy the following code into the script editor. Do not worry about what the code is doing and what it means just yet. This is just an example as to what states are. When copying the code, ensure you copy everything down.

Step 3: Click on save. You will notice once you have clicked save the object will turn green. If you click on the object it turns red. If you click it again the object will turn green again.

NOTE: This example will work, however if you are changing states its best to use touch_end. That way when you are finished touching the object, THEN and only then will it change it state. (Thank you Dave Pearson for bring this to my attention)

How it works

When the script is saved or reset it starts in the default state. Within this default state there are two events. One is for a touch event and the other a state entry event. The state entry event runs as the default state has been entered (state_entry). The code within that event has 1 command llSetColor. This command turns the object green. I will go further into this command later in the book so do not worry if you do not understand it. The second event is the touch_start event. This runs when someone starts to touch the object. The event has 1 line of code reading state red;. This tells the object to enter into the red state (state red).
The red state has the same code as the default state with the difference that it changes the object red not green. The other difference is when the object is touch it calls the state default command telling the object to enter the default state.
 

Events

Events are triggers within your script that run a certain piece of code. There are many different types of events from state_entry and touch_start which we saw earlier, listen and money events. In this book we will learn a few of the events you will need in order to make the products I am teaching you.
An event needs 3 main things. The first is the name. You cannot name these events as they are premade from Linden Labs. The name in the example I am giving are state_entry and touch_start. The second thing it needs is its parameters. These are placed inside the brackets of the event name. A parameter is information which the event is going to use. If we look at the touch_start event we see integer num_detected within the brackets. This is the events parameter. I will go into that a little later. The final thing an event needs is the start and end curly brackets that the state also used.
Let’s go further into some events and describe them a little more. We will also make some objects to demonstrate these events.
 

State Entry Event

The basics of the state_entry event are probably the most simple of all the events. This event does not call for any parameters. Even though no parameters are called for it still needs the brackets, we just leave them empty. The state entry runs the code when the script has entered the state in which this event is inside. In the last object we made we used the state entry event which changed the colour of the object.

Touch Events

There are three different touch events.
touch Runs code continuously while the user is touching the object.
touch_start() Runs code once for each time the avatar starts a click on the object (mouse down)
touch_end() Runs code once for each time the avatar finishes a click on the object (mouse up)

Let’s start by looking at the touch event by making a new object and writing a script.

touch

Step 1: Open up the build window by pressing ctrl and b together. This is an alternative way to open the window from the other way we learnt earlier.
Step 2: Choose a shape to use, any will do. You should notice your mouse cursor has turned into the wand again when it is not over the build window. Click somewhere on the ground near yourself. If your object does not appear ensure you have build rights on the land you are on. If your cursor is not a wand and instead you see a circle with a line through it, it means you do not have build rights. If you do have build rights it may be a Second Life issue or you may be experiencing lag.
Step 3: Name your new object touchEventExample.
Step 4: Click the Content Tab to view the object contents. You should not have anything in here just yet. Click on New Script to create a new script.
Step 5: Right click on the new script and choose Rename. Rename the script touchEventExample. Press Enter. Double click the script to open the script editor.
Step 6: Click inside the editor and press ctrl and A together. This selects all the text available. Now press delete. You should be left with an empty script.
Step 7: Let’s start our default state. Type the following…
 
Step 8: Place your cursor on the line between the two brackets and type the following code…
 
The reason the touch event is indented in is to make it easier to see where things start and end. In this example we can clearly see where the default state starts and ends and where the touch event starts and ends.
The next line we need to add is a say command. This is so we can see something happen when we touch the object.
Step 9: Type in the following code between the touch events curly brackets.
 
Do not worry at the moment how this works. We will go into the say commands a little later in the chapter.

How it works

When you click save the script resets and starts in the default state. Inside the default state it finds the touch event. This event is not activated until the user touches the object. When this happens the object says “Hello!” The object repeats itself while you hold the mouse button down because the touch event runs again and again while the mouse button is held.

There are two other touch events which we can learn the same way. Lucky for us we do not need to write it all again. Follow the next steps to create a duplicate of the last object.
Step 1: Right click on the object we created and select Edit. The build window will appear and the object will have three lines through it. Hover the mouse over one of the lines. You will notice that it lights up a bit more.
 
Step 2: While holding down the shift key on your keyboard, click and drag the object using the lines. When you let go of the mouse button you will notice another object has appeared in the place the old one was.


 
Step 3: Click on the new box. The build window will have the information of the new box. Let’s go ahead and change its name in the general options of the build window. Name this new one touchStartEventExample. Go to the Content window and rename the script to touchStartEventExample also. Right click and choose rename.
Now we have our second object. This object we are going to demonstrate the touch_start event.

touch_start

Step 1: Following straight after the last exercise, double click on the script to open up the script editor.
Step 2: Change the code from

 To
 
Step 3: Click on save and close all the open windows. Now touch the new object. Try to hold the mouse button down.

How it works

This script works very much the same as the last one except the touch_start event is only ran when the user first touches the object. It does not run any more times.

touch_end

Step 1: Copy this object to make a third object. Rename the object and the script touchEndEventExample. If you do not remember how to do this follow the exercise earlier.
Step 2: Change the touch_start or touch event to a touch_end event. Your script should look like the following
 
Step 3: Click save and touch the object. Try to hold the mouse down.

How it works

This script runs the same code as the last script but only executes the code when you lift your finger off the mouse button.
These examples show you the difference between the three touch events. These can be used in many different way and help you create some great scripts. We will be learning two more events within this book but not until a little later.

Commands

There are many commands within SL scripting. They can range from adding variables, saying something in the sim or detecting who has touched an object. In this next section you will be learning a few commands that will help you create your products. The commands are the things that make your object do what you want it to do. All commands must have a semicolon at the end of the line. This is because the script itself does not know where a line starts and ends. We as the scripters must tell the script when an end of line has occurred. A lot of times when a program has an error it may just be missing a semicolon at the end of a command line.

Say Commands

You have already seen the use of a say command. We will now go into the say command in more detail. There are four main say commands which differ in the range in which they can be heard. The smallest ranged command is the whisper command which can be heard within a 10m radius. The next is the most commonly used say command, Say. This one can range up to a 20m radius. Following after that is the shout command. This ranges up to a larger radius of 100m. And the last and loudest command is the RegionSay command. This one can be heard by all avatars on that region.
Command Written Range
Whisper llWhisper(channel, string) 10m
Say llSay(channel, string) 20m
Shout llShout(channel, string) 100m
Region Say llRegionSay(channel, string) Whole Region


These commands all need the same parameters in order to work. They need a channel to talk on and something to say which is in the form of a string. A string, if you have forgotten is a sequence of characters, for example ‘Raiden, ‘Raiden Faxel’ and ‘Hello my name is Raiden’. These examples are all strings. A channel can range from 0 to… well I haven’t found an end number yet. 0 is the channel that all avatars hear. Every other channel is not posted in global chat and so not heard by anyone. These channels are useful for objects to talk to each other. You can have one object say something on channel 12 and another object listening on channel 12. We will go into the listen events later. For the moment let’s stay with the say commands. We won’t go further into the say commands as we will be using them in everything we script. We can learn our lessons on the way.

Detected Commands

In this section we will learn some more commands. These commands are called detected commands and can help to identify things like who touched the object, what face of the object they touched or other information about things happening around the object.

DetectedKey and DetectedName

These are the only two detect commands we will be learning. They both are very useful when combining them with the touch events we wrote earlier. In these following steps we will make your first sellable product. It probably will not be able to be sold for much as there are many of them that are out there for free. The product is a key finder. It will find the UUID of an avatar and display it in global chat. The UUID is a unique code that every object, picture and avatar has in SL. In fact everything in SL has a UUID that is unique. So let’s start on this product.
Step 1: We need a new object. The object I want you to use in this case is the sphere. Press ctrl b to open the build window. Click on the wand and then click on the sphere. Click anywhere on the ground and watch your sphere come into the world.
Step 2: The build window should have gone to the Content tab. If it did not open click the content tab now. Once you have the tab open click on the New Script button. Rename this object and the script “Key Finder”.
Step 3: Open the script editor by double clicking the script. Delete everything that is already in the script. You should now have an empty script editor.
Step 4: Write a new default state.

Step 5: Add a state entry event inside the default state.

 
Remember we need to indent in each time we add a new group of code.
Step 6: We want the object to tell the avatar what to do when it is rezzed out. Add a say command inside the state entry event that will tell the avatar to touch the object to find their avatar key. Your code should look like this.
 
Step 7: Next we need to add a touch start event. Add this below the state entry event.

 
Step 8: Before we go ahead we are going to need a string variable to hold the users key in. To make a string variable we need to come up with a name for it. When naming your variables it is always best to choose something that makes sense. If you name your string x, you will not know what x is in larger scripts. However, if you name your string touchersKey then you will always know what it is. To make this string write the following code inside the touch start event.


Step 9: Right underneath the string write the following code

This line of code tells the script to place the detected key of avatar 0 into the string touchersKey. The number in the detected key command refers to the avatar touching it. If you were using the touch event (the one that repeats the code) and had multiple people touching the objects, you could change this number to refer to a different avatar. 0 is always the first avatar, then 1, then 2, 3,and so on.
Step 10: Now we need to say the key. Let’s make the object whisper the key to the avatar so that it does not bother many avatars. Like I said before a say command needs a channel and a string. In previous uses of the say command we wrote the string ourselves. This time we are just going to give the say command our string touchersKey. Write the following code under the last line you wrote.

 
Your code should now look like this

How it works

This script when it first starts enters the default state. Upon entering that state it runs the state entry event. Inside that event we have written a say command to tell the user what to do. When the avatar touches the object a string variable is created called touchersKey. The next line places detected key of the avatar that touched the object into our touchersKey variable. The last line then whispers our string. Our string holds our avatars key and so the object whispers the avatar key.

Before we declare this product as complete, let’s add some extra features to it. Imagine if ten people all touched the object at the same time. The object will be saying 10 different keys and no one will know which key belongs to whom. So let’s add in something that tells everyone which key belongs to whom. For this to work we are going to use the Detected Name command. This command works the same as the Detected Key command. We will also create another string to hold the name.
This time I will guild you through the steps but will not show you the code. I want you to come up with the code yourself. I will however show you the end result of the code.
Step 1: You need to create a new string variable to hold the avatars name. Let’s call this one touchersName. Place this one above where you created touchersKey.
Step 2: We need to put the avatars name into this new string. It is done exactly the same way as putting the avatars key into touchersKey however instead of using the Detected Key command we will be using Detected Name command.
Step 3: Finally we will need to say the avatars name just before we say the avatars key. Place another whisper command above the first one and get it to say the avatars name.
Step 4: Our code is getting a little bigger so let’s add in some comments. Above where we created the variables add a comment saying ‘Variables’. Above where we placed the information into the variables make a comment that says ‘Variable assigning’. Finally add the comment ‘Tell the user’ above where we use the whisper commands.
Let’s see how well you did. Your code should look like this.

If you got the code right, congratulations. If not do not worry we have a long way to go. I am sure you will pick it up sooner or later.
See how the detected name command is written exactly the same way as the detected key command. Save your script and touch the object. You will see this time the object is now saying one of the following depending on where you are looking at the chat.

Joining Strings

Strings are a very useful variable to use and there are many things you can do with them. In our last exercise we placed an avatars key in a string and their nam
e in another string. It would be a lot easier if we could store them in the same string. Well we can. This is done using a +. Let’s now go back to our key finder and add this in.
Step 1: Create a new string called message. Place the code in the Variable section of your code.
 
Step 2: In the Variable assigning section of the code we need to assign the combined strings into the message string. Change your Variable assigning section of your code to match the following.

You will notice I placed the new line of code below the other ones. This is because we want message to have the avatars name and key. If we do it before they are placed in the first strings our message will be empty. Once message is set it does not change when touchersName or touchersKey changes. We would need to reassign our message the new strings after they have changed.
Step 3: Change our Tell the user section of the code to the following

Step 4: Save the script touch the object. You will see the object now says the name and key on one line.
Raiden Faxele43a6285-1726-4d12-bd31-e194747fee4d
This is of course my name and key. You will receive yours.
This is however very confusing in a practical sense as we cannot see where the name ends and the key starts. It does however show you that using the command, we have joined the two string together and made them one string.
Let’s alter this a little so we can see the difference between the name and the key.
Step 5: Change the message assignment line to read the following.

Notice we have added + “: ” + into the script. This works because anything written inside quotes is seen as a string. In this line of code there are three strings.
  1. touchersKey
  2. touchersName
  3. “: “
The third string cannot be reused as it is not assigned to a variable but we can use it in this line of code. We are then simply adding all three string together.
Step 6: Save your script and touch the object again. This time you will see it say
Raiden Faxel: e43a6285-1726-4d12-bd31-e194747fee4d

This is much easier to read and if multiple people touch the box we can clearly see which key belongs to which avatar.
So we have written our first lsl script. We have learned how to use touch events and use that touch event to collect information on who touched the object. We have also learned how to talk to the avatar or at least tell the avatar the information we have collected.

Next week we will be going into how variables work, what they are and when they will work. Until then, Happy Scripting.

SighFye

No comments:

Post a Comment