Thursday 1 October 2009

To script or not to script

LOL! presents the first in series of articles on the subject of scripting in Second Life by Commander Quandry.

That may be the question you have asked yourself. There is probably no one who has spent more than a day in Second Life that has not at the very least created the basic Box prim. Some will have even increased its size, rotation, or position. Others will have used some of the other properties to taper, slice, twist, cut, hollow, etc their prim to fashion it into something of use or beauty.

Using the edit window and some keyboard and mouse control quite a lot can be done with only one prim. Add others to the prim and objects of greater use can be created. However, in order to really get the benefits that Second Life offers you will want to add functionality to your object. A beautiful lamp is all well and good, but being able to turn it on and off and cast light on a room adds purpose to the design.

LSL provides a means to interact with the server and client systems. The server system is the application running on Linden servers; the client system is the software running on your PC. The scripts are interpreted at run time to fulfil the wishes of the scripter. Some of the functionality is built in and only has to be utilised – like “sit”. Other functionality has to be created.

Some of what we do in scripting is a “cheat” or, in other words, we have used some piece of useful functionality for another purpose, not intended. For instance using the “sit” and “unsit” functionality it is possible to “transport” someone around a SIM. This is not Teleporting in the true sense of Second Life but it usefully replicates teleporting by using the sit/unsit system for another purpose.

The basic elements of LSL are variables, functions, events and handlers. I will explain these a little. However, you will not fully appreciate them until you begin to use them as you script.
Variables

A variable is a word, or joined phrase, that is used to contain information useful to the script. There are different types of variable including integer, string, vector, list and rotation. There are others and we will visit them over the coming months. There are two ways to use variables – global and local. A global variable is available anywhere in the script, a local one is only available in the section of the script it is being used in.

Functions
Functions are the basis of scripting. There are LSL defined functions which form the basis of the scripting language. These functions you will learn as you develop your understanding of LSL. It is also possible to add your own functions. You might have a function that opens a door. You could then call that function when someone “touched” the door, or when they “collided” with the door, or if the owner said, “Open Sesame”. In each instance you would use the same piece of code, your function in the script, to actually move the prim to the open position.

Events and Event Handlers
The main thrust of LSL is for it to be able to react to user input – touching, sitting, talking, etc. Each of these activities create an Event and in scripting we write event handlers to do something when an event occurs. Some are obvious – touch. Some need to be set up – a “listener” for instance to hear when a user says something. We will cover most of these, and give you the tools to work out the rest, over the course of the articles.

The final thing I want to cover in this article is the Basic New Script. This will be a starting point for us and will set the scene.

First rez a Prim – right-click the ground and select create – now left-click the ground. Your prim will appear along with an edit window. Click the “Content” tab of the edit window and click New Script. You will notice that when you created the new script the object said “Hello, Avatar!”. This is because every time you Make a New Script (and an object may have multiple scripts) the Basic Script is added.

Some basic things to note:

· All scripts have a “default” the contents of the default are between the first { in the script and the last }

· All sections of scripts are enclosed in { and }. This script has two parts to the default – state_entry() and touch_start() – again these are each enclosed in { and }

· Each line of code inside a section is terminated with a semi-colon ;
Now let’s take a look at it. Double Click the New Script in the contents.

· state_entry() an event handler that responds to the beginning of an object life. It is fired (events handlers are fired when events occur) when the object is rezzed, the script is created or saved or reset. It uses the llSay function to say “Hello, Avatar!” as we discovered earlier. Let’s look at the llSay function.

· llSay takes two arguments (arguments are pieces of information passed to the function) which are a channel to talk on (an integer) and something to say (a string). Channel 0 is the local chat channel so everyone gets to see the prim say its thing.

· The other event handler is the touch_start and it fires, as you might think, when an avatar touches the object. This handler also contains an llSay which just says, “Touched.”.


By changing the words in quotation marks you can change the greeting the object produces when it is rezzed/touched. Try it for yourself and see what happens!


In the next article we will make a few changes to the script to make the greetings more personal.
I hope that this has been of use, and that it has wet your appetite for more. If you need any in world assistance please IM me “Commander Quandry” or, alternatively email me, adr@rossers.net

No comments:

Post a Comment