Events

From Pointui

It may be confusing to see methods such as Load() which appear to act like events, but are actually methods. So firstly, that needs to be explained … some classes such as Screen and Applet have methods that nearly always require an implementation to be provided. Take the Applet class – an Applet will nearly always need to update something when it’s activated so if the Applet class provides an Activated() method it will be called by the script engine. This could have been provided as an event such as OnActivated() but it would mean every applet you write would need to create an event handler method and then hook up the event to point to it – would work just the same and there is nothing wrong with that approach, except we thought that to make it easier (albeit a little confusing to begin with), we would just get the engine to directly call methods for a lot of the common things. That is why there is a Screen.Load() method that is called (you may be used to see OnLoad events in other languages). If for some reason you need an actual event there is nothing stopping you making one – this is how it can be done for the Load() method of a screen:

class MyAwesomeScreen : Screen
{
	Event OnLoad;
 
	void Load()
	{
		//this Load() method is called once off by the script engine when the
		//control is being initialized
 
		//trigger an event – this allows for normal event handler behavior
		OnLoad();
	}
}

Why would you even bother? Because a true event like the OnLoad one defined above can have it’s handler changed on the fly and can even have multiple handlers assigned, where-as the Load() method will only ever be called directly from the script engine.

Most of the time you will just need to provide the method implementation.

Defining Events

All events must be of type Event, and never include a complete definition such as a parameter list. Basically, an Event is defined and made available to have one or more handlers assigned. Events can never have return values.

Raising Events

An event is raised just by calling it with the same syntax as you would call a method. See the example above. There are no special keywords required.

Event Parameters

Events can include parameters, but make sure you always match up the right number of parameters in both the handler methods and the calls to raise the event. Here is an example:

class Example
{
	Event OnPause;
 
	void Load()
	{
		//setup handler
		OnPause = OnPause_Handler;
	}
 
	void OnPause_Handler(int param1, int param2)
	{
	}
 
	void Pause()
	{
		//raise the OnPause event with some parameters
		OnPause(1234, 5678);
	}
}