ScriptInvoker Usage – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
See Event Handlers for ScriptInvoker guidelines.

A ScriptInvoker is an object that allows other objects to subscribe to the event it represents by registering a method. Such event can provide arguments and it is important for the subscribing methods to fit the proper method signature.

The interest of using Events is that the subscribed method is be executed only if and when a specific event occurs (e.g a fired weapon, someone leaving a vehicle, etc) - no periodical check is done, saving CPU cycles and executing exactly on event.


Usage

Using the ScriptInvoker class directly is considered bad practice and should be avoided!


Signature Declaration

The cScriptInvokerVoid class used above is the "default" one used for methods without arguments. In order to provide arguments to the subscribed methods, a generic ScriptInvokerBase type must be used, e.g cScriptInvokerBase<func>.

A method signature can be declared using typedef:

Copy
void ScriptInvokerIntMethod(int i); typedef func ScriptInvokerIntMethod; typedef ScriptInvokerBase<ScriptInvokerIntMethod> ScriptInvokerInt;

And used like this:

Copy
protected ref ScriptInvokerInt m_OnEventWithIntParameter = new ScriptInvokerInt();

That's it! Remember that ScriptInvokers/Event Handlers are very powerful, but dangerous tools. Be sure not to fall into any trap or loop!