Event Triggered Subroutines


Event triggered subroutines are just what they sound like, they are regular subroutines that can be made to execute when a particular event occurs. They may also be called interrupts, but there is nothing special about the subroutines except they are executed automatically by Basic at just the right moment.

By using event triggered subroutines you eliminate the need to poll events to determine when they happen. This greatly increases speed and responsiveness.

Event Subs have the same restrictions as functions. You can't execute commands that pause execution within an Event Sub. Event subs have to execute in a fraction of a second. There isn't much time to do a lot of things and you certainly can't pause execution with an input statement. If you have to, just set a flag in your interrupt. Then in your main loop, if that flag is true, stop and do the input or whatever takes time.

Any event sub can be named whatever you like. You don't need to follow the names in the examples, like onTouch(). It's the SetOn... command that links the interrupt to the sub. The parameters of an Event Sub are always fixed.

It's also possible to have multiple onTouch() subs and switch from one to another depending on circumstances. In the game engine you may have multiple on-collision subs and link each to different sprites.







Game Engine Events

Become a Patron