Don't click here unless you want to be banned.

LSL Wiki : CrashCourse5

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ia360925.us.archive.org
Chapter Listing
< 1 2 3 4 5 6 7 8 9 >

5) Events
LSL is a modern language, and as such, it supports event-driven programming. This simply means that instead of having to write a program that anticipates all possibilities, it can react to things as they occur.

There is a special class of function called an event handler (though it's more frequently referred to as simply an "event", though when learning, that invariably leads to confusion -- the event should be the thing that happens, and the handler should be what deals with it. Anyway, I digress...). Normally, a program consists of a main (entry) function which in turn calls other functions, and runs in a fairly linear fashion. When you have event-driven programs, however, things can happen out of the blue, and interrupt your program flow. This is actually a good thing. Think about it like this:

Suppose you are reading a good book, but you want a cup of tea to sip while reading. You have at least two ways to manage this:

1) Put the book down, put the kettle on, and watch it until it boils. Make your tea, pick up your book, and enjoy.

2) Put the book down just for a moment, use a whistling kettle, put it on, and resume reading. Once it boils, you interrupt your reading just for a moment to make the tea, and then you can read and sip.

You can probably agree that option 2 is better because it allows you to keep reading while the water is coming to a boil, instead of staring balefully at the kettle until steam is shooting out of it before returning to your book.

If you were to translate this situation to a programming frame of mind, the kettle boiling would be an event, and you would have an event handler that would be called when the boiling event occurred. Simple, no?

Usually, you have to set up an interest in the event to be notified of it. In the case of the kettle, you use a whistle on the kettle, and tune your ears to the whistling sound, so you notice as soon as it happens and can take action.

In LSL, there are quite a few events you can watch for. One very simple event in LSL is the timer. You can set up a timer event, and every x seconds, the timer event handler will be called. Thus, you could make a script that does something on the hour, or whatever. If you want to learn about events practically, however, I recommend you start with touch_start. Lucky for you, it's part of the default code that is added when you create a new script for an object.

Previous | Next

Homepage | Tutorial | CrashCourse | Glossary
There is no comment on this page. [Display comments/form]