ScriptInvoker Usage – Arma Reforger
(Fixed the error with templates) |
Lou Montana (talk | contribs) (Change ScriptInvoker → ScriptInvokerVoid preemptively + warning) |
||
Line 8: | Line 8: | ||
== Usage == | == Usage == | ||
{{Feature|warning|Using the {{Link/Enfusion|armaR|ScriptInvoker}} class directly is considered bad practice and should be avoided!}} | |||
<spoiler text="Show example"> | <spoiler text="Show example"> | ||
Line 13: | Line 15: | ||
class TAG_Dog | class TAG_Dog | ||
{ | { | ||
protected ref | protected ref ScriptInvokerVoid m_OnBark; // initialised on request - this allows saving RAM as ScriptInvokers are not small objects | ||
// IN GENERAL, do NOT expose the ScriptInvoker variable directly (as anyone could delete it) | // IN GENERAL, do NOT expose the ScriptInvoker variable directly (as anyone could delete it) | ||
// Arma Reforger code keeps Component events public for performance reason | |||
//! This method provides the ScriptInvoker for outsiders to subscribe to it | //! This method provides the ScriptInvoker for outsiders to subscribe to it | ||
ScriptInvokerVoid GetOnBark() | |||
{ | { | ||
if (!m_OnBark) | if (!m_OnBark) | ||
m_OnBark = new | m_OnBark = new ScriptInvokerVoid(); // the invoker is only created on external request | ||
return m_OnBark; | return m_OnBark; | ||
Line 73: | Line 76: | ||
== Signature Declaration == | == Signature Declaration == | ||
The <enforce inline> | The <enforce inline>ScriptInvokerVoid</enforce> class used above is the "default" one used for methods without arguments. | ||
In order to provide arguments to the subscribed methods, a generic {{Link/Enfusion|armaR|ScriptInvokerBase}} type must be used, e.g <enforce inline>ScriptInvokerBase<func></enforce>. | In order to provide arguments to the subscribed methods, a generic {{Link/Enfusion|armaR|ScriptInvokerBase}} type must be used, e.g <enforce inline>ScriptInvokerBase<func></enforce>. | ||
Revision as of 18:03, 19 June 2023
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
Signature Declaration
The ScriptInvokerVoid 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 ScriptInvokerBase<func>.
A method signature can be declared using typedef:
And used like this:
That's it! Remember that ScriptInvokers/Event Handlers are very powerful, but dangerous tools. Be sure not to fall into any trap or loop!