ScriptInvoker Usage – Arma Reforger
Lou Montana (talk | contribs) m (Text replacement - "\{\{GameCategory\|armaR\|Modding\|(Guidelines|Tutorials)\|([^=↵]*)\}\}" to "{{GameCategory|armaR|Modding|$2|$1}}") |
Lou Montana (talk | contribs) (Fix example, thanks Zelik/SweetPotatoh on Discord) |
||
| Line 117: | Line 117: | ||
class TAG_Subscriber | class TAG_Subscriber | ||
{ | { | ||
protected TAG_EventHolder m_EventHolder; | protected ref TAG_EventHolder m_EventHolder; | ||
protected void WarnOfScoreChange(int newScore) // note the similarity with ScriptInvokerIntMethod's signature | protected void WarnOfScoreChange(int newScore) // note the similarity with ScriptInvokerIntMethod's signature | ||
| Line 129: | Line 129: | ||
UnSubscribe(); | UnSubscribe(); | ||
eventHolder.Insert(WarnOfScoreChange); // if the method signature were not matching, the compiler would warn here | eventHolder.GetOnScoreChanged().Insert(WarnOfScoreChange); // if the method signature were not matching, the compiler would warn here | ||
m_EventHolder = eventHolder; | m_EventHolder = eventHolder; | ||
} | } | ||
| Line 138: | Line 138: | ||
return; | return; | ||
m_EventHolder.Remove(WarnOfScoreChange); | m_EventHolder.GetOnScoreChanged().Remove(WarnOfScoreChange); | ||
m_EventHolder = null; | m_EventHolder = null; | ||
} | } | ||
Latest revision as of 20:35, 23 February 2026
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!