CHAPTER 2
CHAPTER 3
CHAPTER 4
CHAPTER 5
CHAPTER 6
CHAPTER 7
CHAPTER 9
CHAPTER 10
CHAPTER 11
CHAPTER 12
CHAPTER 13
CHAPTER 14
CHAPTER 15
APPENDIC
E
S
25
CHAPTER 1
EVENTS AND EVENT HANDLERS
LSL is an event-driven language. This behavior is a major discriminating feature between LSL and many
other languages. Essentially, the script's flow is determined by events such as receiving messages from other
objects or avatars, or receiving sensor signals. Functionally, events are messages that are sent by a Second
Life server to the scripts active in that sim. Event messages can be received by defining the matching event
handlersJOUIFTUBUFTPGZPVSTDSJQUTNothing happens in a script without event handlers: even the passing
of time is marked by the delivery of timer events. Some events cannot happen without the matching event
IBOEMFSBDUJWFTPNFXIFSFJOUIFPCKFDUJOBUMFBTUPOFPGJUTQSJNT'PSJOTUBODFJGBOPCKFDUEPFTOhUIBWF
a script with a money() event handler, avatars cannot pay that object.
Many library functions have effects that take relatively long periods of time to accomplish. Rather
than blockingTUPQQJOHFYFDVUJPO-4-VTFTasynchronous communications: your script fires off a request
that something happen, and it is notified with an event when the request is satisfied. This pattern is used
BOZUJNFZPVSTDSJQUNBLFTBDBMMUPBMJCSBSZGVODUJPOUIBUDBOOPUCFIBOEMFEMPDBMMZCZUIFIPTUTJNGPS
instance, the function llHTTPRequest() and the http_response() event handler), when physical
TJNVMBUJPOFGGFDUTBSFOhUHPJOHUPCFJOTUBOUBOFPVTllTarget() and at_target()). You could also
UIJOLPGSFQFBUJOHFWFOUTllSensorRepeat() and sensor()PSFWFOMJTUFOFSTllListen()
and listen()) as following the same model: make a request and get responses later. This model of
asynchronous execution allows your script to keep functioning, handling other events and interactions
without stopping to wait for long-term requests to happen.
"TFWFOUTPDDVSBQQMJDBCMFPOFTUIPTFUIBUIBWFIBOEMFSTEFGJOFEPOUIFDVSSFOUTUBUFBSFRVFVFE
for execution by the script in the order they were raised. You should be aware of several important
constraints on the delivery of event messages to handlers:
r #ZEFGBVMUFWFOUTBSFEFMJWFSFEUPBTDSJQUOPNPSFGSFRVFOUMZUIBOFWFSZTFDPOE:PVDBOBEKVTU
this with llMinEventDelay(float seconds) but it can not be less than 0.05 second.
r "NBYJNVNPGFWFOUTDBOCFRVFVFEPOBTDSJQUBOZBEEJUJPOBMFWFOUTXJMMCFTJMFOUMZ
discarded!
r 2VFVFEFWFOUTBSFTJMFOUMZEJTDBSEFEXIFOBTDSJQUUSBOTJUJPOTCFUXFFOTUBUFT
The combination of these factors means that if you have code that expects to get events rapidly and
UBLFTBSFMBUJWFMZMPOHUJNFUPFYFDVUFJODMVEJOHBSUJGJDJBMEFMBZTZPVNBZSVOUIFSJTLPGNJTTJOHFWFOUT"T
BOFYBNQMFTFOEJOHBO*.XIJDIEFMBZTUIFTDSJQUCZTFDPOETJOSFTQPOTFUPDPMMJEFFWFOUTXIJDIDBO
happen as often as every 0.1 second) is probably asking for trouble.
Similarly, the LSL function llSleep(float seconds) pauses script execution for the specified
number of seconds without leaving the current event handler, similar to the way that many LSL functions
introduce artificial delays. In both cases, there is a potential problem if your script is likely to handle lots
of events.
Some events are delivered to a script only if the script has made the appropriate request. For
instance, a listen() event handler is never called unless another handler in the same state has called
llListen(). The SYW website has a list of all the LSL event handlers, the functions required to enable
the event, and the Second Life situation that results in that raised event. And, of course, there are lots of
examples throughout the book.

Get Scripting Your World: The Official Guide to Second Life® Scripting now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.