Monday 3 May 2010

Keep out!

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.

No comments:

Post a Comment