282 Virtual World Design
2. When that is created, you should see “Script running” (OpenSim) or “Hello, Avatar!” (Second Life)
appear in your local chat panel or on the lower left of the screen. Congratulations—you just created
your rst script, and it made the object talk to you.
14.4.2 The sCripT TexT ediTor and iTs parTs
Before we dive into the actual script writing, let’s look at the parts of this window called the Script Editor
(Figure14.4). Double click on the new script in the context window to open the Script Editor. Although it
FIGURE 14.4 Screen shot of Script Editor menu showing (1) text entry window, (2) status box and error message
window, (3) location indicator for cursor in text window showing line and column numbers, (4) drop down menu for
inserting new functions, constants, and other script elements, (5) toggle for Mono, an open source scripting engine that
can speed up the performance of a script and (6) opens up WordPad for editing and saving script as a document.
283Scripting Basics for the Designer
looks simple, this interface has signicant functionality built into its parts. The Script Editor is a text editor
and a compiler that translates (compiles) the textual symbols we use to write the script into a much more
compact code for the server to run.
14.5 BREAKDOWN: FINDING THE PARTS OF A BASIC SCRIPT
If you look in the Script Editor window, and you have added a new script into an object on a virtual world
running OpenSim, you will see what is shown in the two left-hand columns of Table14.1. Initially, this writ-
ing probably looks like alien transdimensional driving instructions, but if you take it in bit by bit, so to speak,
itwill start to make sense. Remember the movie script sample and how that delineated between what was
going on and who was saying it? That is happening here. In all the script examples shown in Tables14.1 to 14.5
the actual script is written in the left-hand column and the “plain English” description of what the program is
supposed to do is in the right-hand column for your comparison. Let’s examine these two initial scripts from
Second Life and OpenSim, line by line, starting with the one made inside a prim in the OpenSim environment.
14.5.1 a BasiC sCripT in opensim
In Table14.1 is the starting script for an object in OpenSim, broken down by line and dened in plain English
so you can see what those lines of code represent. When you read these scripts, pay close attention to the
curly brackets { } because they enclose important parts of the script and tell the virtual simulation that inter-
prets and runs the script where each section starts and ends.
Line 0 says default. What this means is that the object’s script is in its default state when the script
starts. States are the fundamental part of scripting; each object may be in only one state at a time but
may transition through many in the course of running a script, if necessary. With the word default,
the script is saying to the server, enter the rst state and “start here.” If you look at this script on your
screen, you will see that the text is red. The Script Editor color codes various parts to help identify
them. Default states are always indicated in red.
Line 1 shows the{symbol, which is a curly bracket, and if you follow along in the script editor and
click your mouse cursor on it, it will indicate “Line 1, Column 1” just below the status box. This
bracket encloses all the event handlers, functions and other scripting components that happen when
the script enters its next state, where it will actually begin to do something. Events (and the event
handlers that deal with them) come in many forms; they can be “listen” events that respond to some-
thing in the chat, “sensor” events that respond to avatar proximity, or “collision” events that respond
to something colliding with the object containing that script, to name a few.
TABLE14.1
A Basic Script in OpenSim
Line No. Script Code What It Means in English
0
default
“Start here.
1
{
This is where we start to have Events and Functions.
2
state_entry()
Event: transition to a new State and wait for instructions.
3
{
This is a new block of code.
4
llSay(0, “Script running”);
Function: say in local chat channel 0, “Script running.
5
}
This is the end of a block of code.
6
}
All Events and Functions end here, and it is the end of the State.
284 Virtual World Design
Line 2 says state _ entry (). This is an event handler, the scripted receiver specic to an event
that tells the server to execute the commands between the curly brackets { and } of the next text
block. The state_entry () event handler activates when the script runs for the rst time (an event)
or when the script is saved or reset (an event). Please note, in general practice this script element is
simply referred to as an “event,” rather than an event handler. The event color code is blue in the
Script Editor. A state is one of the basic parts of LSL scripting, and a script can only be in one state
at any given time. In this tiny script, there are two states, default, and state_entry. States contain
events by enclosing them with { and } in the structure of LSL.
Line 3 shows{, another curly bracket, but further right in the column. It has been shifted over to indi-
cate to the scripter and to the computer that within this new state, there will be some more things
to do. The curly bracket is saying that these new instructions will happen during this new state and
only while the cube is in this new state.
Line 4 says llSay(0,Script running”). This is a function named llSay(), followed by an
indication of what chat channel the cube will announce the message “Script running” on. In this case,
the channel is 0 and is indicated by the rst number (or integer) in the line of code. The color code for
functions is red in the Script Editor. A function is a member of a very large family of commands that
do specic things in scripts. If you move your mouse down and click on the insert drop-down box
below the status window, you will see a whole list of words starting with ll, and they are all functions
available for use in scripts. Also, note that they have a name llSay(), for instance, and are followed
by (). Note that this line ends with a semicolon, which is required for lines with functions in them.
Line 5 is the}or curly bracket to close the little code block that holds the llSay() function and its
trailing number (or integer) for the chat channel and the string of text it has to say.
Line 6 is}and closes the state within which the cube performs its function of letting us know that the
script is indeed running.
So, to recap what happened: The cube was made, and a new script was added. When that script was added,
it opened in its default state. Then, the cubes script entered a new state, ran a function that made the cube say
that the script inside the cube was running.
14.5.2 a BasiC sCripT in seCond life
Now, for a slightly more complex starting script, let’s take a close look at the starting script for a cube made
in Second Life. Refer to the left two columns of Table14.2.
As you follow along in your on-screen Script Editor looking at a starting script loaded into a cube made in
Second Life, the rst ve lines should look very familiar. The basic start script for Second Life does the same
things as the start script in OpenSim. However, instead of saying “Script running,” it says “Hello, Avatar!”
in the llSay() Function.
After the blank line on line 6, we see something new.
Let’s analyze the rest of this script. If the mouse cursor is hovered on the lines while the Script Editor is
open, the Firestorm viewer will give you hints regarding the functionality of each script element and its usage.
Line 7 tells you that this is an event; touch _ start (integer total_number) will activate the follow-
ing function each time the cube is clicked by your mouse, or “touched” (an event).
Line 8 has the curly bracket{, indicating that we will have the start of another block of code in the script.
Line 9 has our old friend the function llSay() followed by the channel number, which is 0 in this
case, and the statement Touched. as well as the nal semicolon that ends the line.
285Scripting Basics for the Designer
Line 10 closes that script block with the curly bracket}.
Line 11 closes the entire script and sends the script back to the default or standby, where it will wait
for another avatar’s touch to activate the touch _ start event and its llSay() function again.
14.5.3 CreaTing a sCripT using auTosCripT
Now, let’s try making a script of your own with a little help from an online script generator. This process and
the script it generates can be used in either OpenSim or Second Life.
Here are the steps:
Step 1. Repeat the same procedure as previously and generate a new cube prim or object with a new
script in it. Click on the New Script in the contents and open the Script Editor again. It should have
the same new script in it that the previous one did.
Step 2. Open your web browser and go to http://www.3greeneggs.com/autoscript/. Autoscript is the
brilliant creation of Hilary Mason, a data scientist in her rst life; she goes by the name of Ann
Enigma in Second Life. As she describes in the introduction, the site “tries to map the way you think
into the way the LSL interpreter thinks.” Using this website will show you new ways to learn about
scripting and how to think like a scripter. Autoscript gives you two sections of options. The top one
covers some basic tasks that you would want the script to do, and the bottom one concerns itself with
the activation and timing of that action.
For our rst use of this, let’s keep it simple.
Step 3. In the Script Editor window, select all of the old script in the text window and delete it out; you
will be pasting in the new autoscript script and need a clean slate.
Step 4. Go back over to the autoscript window. Pick the radio button that says “change the object’s
color” in the top list and select “red” on the color drop-down text box that appears. Then, pick
“when an avatar touches your object” in the bottom list. Click on the Make My Script button to
generate the script. Select all of the new script that appears and copy/paste it into the empty script
window. The new script should look like the code on the left side of Table14.3.
TABLE14.2
A Basic Script in Second Life
Line No. Script What It Means in English
0
default
“Start here.
1
{
Start of the script.
2
state_entry()
Event: transition to a new State and wait for instructions.
3
{
This is a new block of code.
4
llSay(0, “Hello, Avatar!”);
Function: say in local chat “Hello, Avatar!”
5
}
End of the block of code.
6 Blank Line.
7
touch_start(integer total_number)
Event: do something when you are touched, activate the Function.
8
{
Next block of code starts.
9
llSay(0, “Touched.”);
Function: say in local chat “Touched.
10
}
End of previous block of code.
11
}
All Events and Functions end here, and it is the end of the script.
Return to default State.
286 Virtual World Design
This one is 15 lines long because it uses extra spaces to keep the script clear and easy to read. Let’s go
through the lines again and look at the new things that have appeared.
Lines 0 to 1 are where Ann Enigma has made some comments about the script and where it came from.
When a scripter puts a double backslash in front of any line in these scripts, the lines will turn into
orange color-coded “comments,” which will not be read by the compiler as part of the active script.
This gives the scripter a place to add instructions and comment on the various aspects of more
complex scripts.
Lines 24 are Blank Lines to keep the script clean and neat.
Line 5 contains our old friend default, the built-in command that sets up the initial state of the script.
Thecube is now waiting for an event to react to in the next state.
Line 6 contains the curly in bracket indicating the script block and new state will be starting.
Line 7 is blank for ease of reading the script.
Line 8 starts the now familiar touch _ start event, color coded as usual in blue. The line ends with
another curly brace, shown like this{,and here is where the next script block will be starting.
Line 9 is left blank for clarity.
Line 10 has another comment. It says//c h a n g e c o lo r! Here, the scripter is indicating that the next
line contains a function that will change the color of the cube.
TABLE14.3
An Autogenerated Script
Line No. Script What It Means in English
0
//This script was auto-generated by AnnEnigma’s
script autogenerator
Notes about how the script was made. Will
not be read because of // at the beginning.
1
//available at http://www.3greeneggs.com/autoscript/
Notes about where you can access the
Autoscript generator online.
2 Blank Line.
3 Blank Line.
4 Blank Line.
5
default
“Start here.
6
{
New block of code, start of new State.
7 Blank Line.
8
touch_start(integer total_number) {
Event: do something when touched, and
start of next block of code.
9 Blank Line.
10
//change color!
Notes about what will happen.
11
llSetColor(<1.0,0,0>,ALL_SIDES);
Function: change the color to an RGB
percentage, on all sides (a CONSTANT).
12 Blank Line.
13
}
End of block of code.
14 Blank Line.
15
}
Script over, go back to default.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset