Tuesday, 12 May 2015

Secondlife - Learn to Script: Chapter 3

Contents

Chapter 2: Let's Make A Key Finder
Chapter 3: Let's Make A Personal Microphone
Chapter 4: Let's Make A Donation Box

Chapter 3: Let's Make A Personal Microphone

Scope of Existence 

Before we start I want to explain the scope of existence. The scope of existence refers to where a variable is valid in comparison to where it was created. A variable is only valid inside the area it was created in. Let’s take a look at the following example.
V was created inside the script but outside everything and so can be used anywhere in the script.
W was created inside the default state. It can be used anywhere within the default state, but it cannot be used in the newState as this is outside its scope of existence.
X was created inside the touch start event of the default state. It can only be used inside this touch event. If newState had a touch start event also, X cannot be used inside that touch event.
Y was created inside the while loop. It can only be used inside this while loop. If you tried to use it elsewhere you would get an error.

Lets Get Started

We have now come to where we will be making our next product and learning the next command and event. The listen event and commands are useful in making our objects talk to each other or even make it so you can talk to your object. Let’s just jump into the deep end on this one and get scripting.
Step 1: Make a new object with a script inside. Call this one “Personal Microphone”. This object we are making is a wearable microphone. When turned on it will shout everything you say. Delete everything in the script to start with an empty script.
Step 2: In this script we are going to have two states. The first being default which is always needed. We are going to treat this as the off state. The second state we need is the on state. Write the two states to get your script started. Your script should look like this
Step 3: We need an event to change from default to on. Let’s set that up with a touch event. Write a touch start event inside each state.
Step 4: We need to tell the script to switch to the other state inside the touch event. If you remember how to do this go ahead and add it in. If not, just look below.
Step 5: Add a state_entry event for each state. The state_entry event runs when the state is entered. So switching to the on state will run the on state's state_entry command. This is written as followed.
Step 6: The last step in setting up this product before we discuss the listen commands is to add in the say command to tell what state the microphone is in.


This is as far as we are going to go for now. Before we continue I need a talk about the listen command and event.

Listen Command

The listen command tells the script to start listening on a set channel and also what things to listen to or listen for. A listen command is written in the following manner.
The parameters are the listen filters. Channel is of course the channel on which we want the script to listen on, ObjectName is the name of the object or avatar we want the script to listen to, TalkerID is the UUID of the avatar we want the script to listen to, and MessageSpoken is the message we want the script to listen for.
Any of these filters we can leave empty in a way to tell the script to listen to everything in that category. For example, if I want the script to listen to any object I would type empty quotes in that filter. If I want to listen for any message I would type empty quotes in that field. The only filter that needs a true input is the channel.
These three listen commands listen for different things. The first one will listen on channel 0 for anything said by any object or any avatar. The second will listen on channel to for an object called “Object” saying anything. The last line will listen on channel 5 for the owner of the object containing the script, saying “Hello”.
The llGetOwner() command will get the UUID or key of the owner that owns the object that the script is in. The NULL_KEY tells the script not to listen to any set key. Basically it makes the key reference empty.
In our microphone script we want to listen to anything the owner of the microphone is saying in global chat. Add this line into the script in the state entry event of the on state.
You code will look similar to this

You can have many listen commands all sent to the save script.
So now we have the script listening to the owner. It can hear everything the owner says. But it will not do anything when it hears it. We have not set up an event to tell the script what to do when it hears the owner. This leads into our next section.

Listen Event

The listen event is executed when the object hears something that fits the listen command filters you set. I listen event has the same parameters as the listen command and are set in the same order. This is how it is written.
All the listen commands will be sent to the same event. The parameters are stored in the variables created in the event eg: integer channel, string name…
Let’s add this into our script and test it out with some say commands in there.
Step 1: Place the listen command under the touch event
Step 2: Add 4 say commands inside the listen event (one for each variable).
Remember, the reason we placed (string) in front of some of these variables is because the say command can only say strings. The variables name and message are already string but we need to cast the integer and the key into strings.
Step 3: Save your script
Step 4: Touch your object to turn it on and then say something.

How it works

Our script starts in the default state. When we touch the object it switches to the on state. When it enters this state the script goes into the state entry event. Inside this event we have a say command telling the avatar the microphone is now turned on. We also have the listen command. This sets the script to listen on channel 0, to any name, to only the owners UUID or key and to listen to anything they say.
How the script is set to listen to anything. We then said something in the global chat. I personally said “Hi”. This starts our listen event, which has our 4 say commands inside it.
What you will notice is the object saying
The first say command is told to say the channel. We spoke in the global channel which is 0. As you can see the object has said 0. Next it was myself, or you in your case, that said something. Our next say command was to say name. In my case it said Raiden Faxel. The following say command was to say the id. The id is the UUID of the avatar or object saying it. My this case is it my UUID or yours in your case. And finally the last say command was to say the message. This is what we actually said. I said Hi. The script has repeated what I have said.
This should hopefully give you an understanding as to what the listen command and event does. There are only a few changes we need to do to finish this
Step 1: In the listen event we just created delete the first 3 say commands leaving only the one that says the message.
Step 2: Change this say command to a shout command.
Step 3: Let’s add some comments to help you understand what is going on.
 
Step 3: Save your script.
Now we just make your object useable. I personal set it to attach to my ear and make it look like a personal microphone. What you make yours look like is entirely up to you.
Congratulations you just made your second product.
The next section of this book we will be learning to do tests on information we are given and run different code from the different results. These are called if statements. For example: If the avatar touching the object is the owner, show them the menu, otherwise, tell them access denied.

With our second product made, I hope you are starting to learn how things work. Next week, we will work on another product a donation box. We will be learning how to receive and send money to and from avatars. 
Until Then Geek It Up

SighFye

No comments:

Post a Comment