Commander Quandry brings us the latest in his series of scripting articles.
Keep Out!
Something I have been asked about a few times over the last few weeks is the locking of doors – or more to the point, making a door only work for the owner.
This technique is simple to implement and, since it is done through the touch event, works to stop anything working unless it is the owner doing the touching. So you can stop others from switching on your lights, or starting your swing, etc.
The code we will use is put into the touch_start event method for this example. However, it could equally be used in a collision event, or listen event, for objects that respond to being collided with or spoken to.
Create yourself a prim and add the New Script to it. We will edit the touch_start, that is already present.
The basic touch_start looks like this:-
touch_start(integer total_number)
{
llSay(0, "Touched.");
}
When the object it touched it says – Touched – in local chat. We will change this so that it says hello to it’s owner by saying, “Hello Master”. And to anyone else it will say, “Go away, you are not my Master”.
There are two functions we will use for this. Before explaining those just a word about the touch_start event. This event is designed to be able to handle several people touching it at once (or within an event cycle). The integer total_number identifies the number of touchers. We will not be using it for this example, but if our object we a notecard giver, with multiple choices of notecard, this allows the object to present the menu of notecard to several people and respond to the right person in each case.
The first function we will be using is the llDetectedKey function. This takes one parameter, an integer, and works for touch or collide. This parameter is asking the function to tell it who was the person who touched it, and the integer says which person touching we want. In our example we are only interested in the first person touching the object, so we pass 0 as the parameter.
The second function is llGetOwner and returns the key of the objects owner. By comparing these we can decide whether it is the owner doing the touching. Let’s take a look at the script:-
touch_start(integer total_number)
{
if (llDetectedKey(0) == llGetOwner())
{
llSay(0, "Hello Master");
}
else
{
llSay(0, "Go away, you are not my Master");
}
}
It is fairly simple to see what is going on here.
On touching an object the touch_start event method runs. We compare the llDetectedKey with the llGetOwner and if they are the same we use llSay to say hello to the owner. If they are not the same it says something different.
By using this comparison we can open a door, or start a swing, or anything else we want to do on the basis of ownership.
This has been a short article because the technique is simple – however it can be used very effectively for lots of things.
As always please contact me if you need any assistance with scripting. Commander Quandry in world, adr@rossers.net out of world.
Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts
Monday, 3 May 2010
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.”.
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
Subscribe to:
Posts (Atom)