LSL 101/Event Handler Parameters

From Second Life Wiki
< LSL 101
Revision as of 15:57, 7 July 2012 by Toady Nakamura (talk | contribs) (header fix)
Jump to navigation Jump to search
← The Structure of a Script ↑̲  LSL 101  ̲↑ In the Beginning →

When we introduced the touch_start event handler, we put off explaining the meaning of what was between the parentheses in this line:

<lsl> touch_start( integer num_detected )</lsl>

The time has come when we can easily explain this. The phrase integer num_detected is a declaration of a local variable that has been created and initialized by the sim server. The server initializes it with the number of distinct touches that have occurred since the last time the touch_start handler was called.

Consider this script:

<lsl>integer TotalTouches = 0;

default {

    touch_start( integer num_detected )
    {
         // Update the total number of touches
         TotalTouches = TotalTouches + num_detected;
         // Announce the current total
         llOwnerSay( "I have been touched a total of " + (string)TotalTouches + " times." );
    }

}</lsl>

We have a global variable TotalTouches which is accumulating the total number of times our object is touched. Each time the touch_start event handler is called, the sim server initializes num_detected to be the number of times the object was touched since the previous execution of touch_start. If you test this by yourself, chances are that TotalTouches will always be incrementing by one. But if you get a bunch of your friends together and have them all touch the object as rapidly as they can, sometimes you will see TotalTouches incrementing by more than one.

There are many event handlers (about 35 different ones in all) and most of them have one or more parameters. The details will differ with the event, but the idea is always the same. Each event handler handles one particular type of event and the parameters in the event handler provide the script with the specific details.

On a side note, we see from this example that a script doesn't have to have a state_entry event handler. A script has to have at least one event handler, but if nothing needs to be done when the script starts up, it isn't necessary to put in an empty state_entry handler.