addPublicVariableEventHandler: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "]]<dd class="note">" to "]]</dt> <dd class="note">")
m (Some wiki formatting)
Line 3: Line 3:
|game1= arma1
|game1= arma1
|version1= 1.09
|version1= 1.09
|game2= arma2
|version2= 1.00
|game3= arma2oa
|version3= 1.50
|game4= tkoh
|version4= 1.00
|game5= arma3
|version5= 0.50


|arg= global
|arg= global
Line 19: Line 31:


This event handler will detect if a [[missionNamespace]] variable (it is attached to) has been broadcast over network with [[publicVariable]], [[publicVariableClient]] or [[publicVariableServer]] commands and will execute EH code upon detection. Arguments passed to the code in ''_this'' array are:
This event handler will detect if a [[missionNamespace]] variable (it is attached to) has been broadcast over network with [[publicVariable]], [[publicVariableClient]] or [[publicVariableServer]] commands and will execute EH code upon detection. Arguments passed to the code in ''_this'' array are:
<br>
* _this select 0: [[String]] - broadcast variable name (same variable name EH is attached to)
<br>_this select 0: [[String]] - broadcast variable name (same variable name EH is attached to)
* _this select 1: [[Anything]] - broadcast variable value  
<br>_this select 1: [[Anything]] - broadcast variable value  
* _this select 2: [[Object]], [[Group]] - target the variable got set on with [[setVariable]]  (see {{HashLink|#Alternative Syntax}})
<br>_this select 2: [[Object]], [[Group]] - target the variable got set on with [[setVariable]]  (see: alternative syntax)
{{Feature | Informative |
{{Feature | Informative |
* This EH works '''only''' in Multiplayer environment
* This EH works '''only''' in Multiplayer environment
Line 37: Line 48:
|p2= code: [[Code]] - By default, the code is executed in [[missionNamespace]]
|p2= code: [[Code]] - By default, the code is executed in [[missionNamespace]]


|r1 [[Nothing]]
|r1= [[Nothing]]


|s2= varName [[addPublicVariableEventHandler]]  [target, code] {{Since|arma3|1.46|yes}}
|s2= varName [[addPublicVariableEventHandler]]  [target, code] {{Since|arma3|1.46|yes}}
Line 65: Line 76:
|seealso= [[publicVariable]], [[publicVariableClient]], [[publicVariableServer]]
|seealso= [[publicVariable]], [[publicVariableClient]], [[publicVariableServer]]
}}
}}
{{GameCategory|arma2|Scripting Commands}}
{{GameCategory|arma3|Scripting Commands}}
{{GameCategory|tkoh|Scripting Commands}}


<dl class="command_description">
<dl class="command_description">
Line 79: Line 86:
As a result, the example eventhandler on this page will only fire when the variable publicThis has been changed,
As a result, the example eventhandler on this page will only fire when the variable publicThis has been changed,
but not if any other variable was changed by any other client via the publicVariable command.  
but not if any other variable was changed by any other client via the publicVariable command.  
</dd>
<dt><dt>
<dt><dt>
<dd class="notedate">Posted on 27 Feb, 2014 00:57</dd>
<dd class="notedate">Posted on 27 Feb, 2014 00:57</dd>
Line 84: Line 93:
<dd class="note">
<dd class="note">
Note on using addPublicVariableEventHandler during initialization: If you need a function to call addPublicVariableEventHandler during initialization, you must use postInit. addPublicVariableEventHandler does not work during preInit.
Note on using addPublicVariableEventHandler during initialization: If you need a function to call addPublicVariableEventHandler during initialization, you must use postInit. addPublicVariableEventHandler does not work during preInit.
</dd>
<dt><dt>
<dt><dt>
<dd class="notedate">Posted on 27 Feb, 2014 08:40</dd>
<dd class="notedate">Posted on 27 Feb, 2014 08:40</dd>
Line 89: Line 100:
<dd class="note">
<dd class="note">
MulleDK13 note above needs some clarification. You don't "must" use postInit and you absolutely can use preInit function to initialise [[addPublicVariableEventHandler]] if you start [[Scheduler#Scheduled_Environment|scheduled]] script from it. <br>
MulleDK13 note above needs some clarification. You don't "must" use postInit and you absolutely can use preInit function to initialise [[addPublicVariableEventHandler]] if you start [[Scheduler#Scheduled_Environment|scheduled]] script from it. <br>
<code>//script with preInit = 1; in CfgFunctions
<code>{{cc|script with preInit = 1; in CfgFunctions}}
0 = 0 [[spawn]] {
0 = 0 [[spawn]] {
"someVar" [[addPublicVariableEventHandler]] {
"someVar" [[addPublicVariableEventHandler]] {
//yourcode
{{cc|yourcode}}
};
};
};</code>
};</code>
</dd>
<dt></dt>
<dt></dt>
<dd class="notedate">Posted on November 23, 2015 - 21:36 (UTC)</dd>
<dd class="notedate">Posted on November 23, 2015 - 21:36 (UTC)</dd>
Line 105: Line 118:
     [[publicVariableServer]] "OnServer";
     [[publicVariableServer]] "OnServer";
};</code>
};</code>
<code>// This example assumes the client knows their own client ID
<code>{{cc|This example assumes the client knows their own client ID}}
// It does also work on the server (when the server ID is used) irrespective of the command name
{{cc|It does also work on the server (when the server ID is used) irrespective of the command name}}


"OnClient" [[addPublicVariableEventHandler]] { [[hint]] "This event handler still fired!"; };
"OnClient" [[addPublicVariableEventHandler]] { [[hint]] "This event handler still fired!"; };
Line 112: Line 125:
</code>
</code>
</dd>
</dd>
</dl>
</dl>

Revision as of 01:16, 13 June 2021

Hover & click on the images for description

Description

Description:
Arma 3
This command is deprecated since the introduction of remoteExec and remoteExecCall.

This event handler will detect if a missionNamespace variable (it is attached to) has been broadcast over network with publicVariable, publicVariableClient or publicVariableServer commands and will execute EH code upon detection. Arguments passed to the code in _this array are:

  • This EH works only in Multiplayer environment
  • There is no way to remove this EH
  • This EH will not fire on the machine that executed broadcast with publicVariable command, only on the machines that receive the broadcast
  • The value of broadcast variable can be exactly the same, it is the actual broadcast that triggers EH not the change in variable
Problems:
Alt syntax of this command doesn't work as intended.
Groups:
Broken CommandsEvent HandlersMultiplayerVariables

Syntax

Syntax:
varName addPublicVariableEventHandler code
Parameters:
varName: String - missionNamespace variable name
code: Code - By default, the code is executed in missionNamespace
Return Value:
Nothing

Alternative Syntax

Syntax:
varName addPublicVariableEventHandler [target, code] Template:Since
Parameters:
varName: String - name of the variable set on target with setVariable
[target, code]: Array
target: Object, Group or Team Member
code: Code
Return Value:
Nothing

Examples

Example 1:
"publicThis" addPublicVariableEventHandler { hint format [ "%1 has been updated to: %2", _this select 0, _this select 1 ] };
Example 2:
Client:"'^:)123BURP,+=lol" addPublicVariableEventHandler {hint ("NUTS are " + (_this select 1))}; Server:missionNamespace setVariable ["'^:)123BURP,+=lol", "craZZZZy"]; publicVariable "'^:)123BURP,+=lol";

Additional Information

See also:
publicVariablepublicVariableClientpublicVariableServer

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Posted on 22 Dec, 2007
TeRp
Please note that varName indicates which variable you want to monitor with this eventhandler.
As a result, the example eventhandler on this page will only fire when the variable publicThis has been changed, but not if any other variable was changed by any other client via the publicVariable command.
Posted on 27 Feb, 2014 00:57
MulleDK13
Note on using addPublicVariableEventHandler during initialization: If you need a function to call addPublicVariableEventHandler during initialization, you must use postInit. addPublicVariableEventHandler does not work during preInit.
Posted on 27 Feb, 2014 08:40
Killzone_Kid
MulleDK13 note above needs some clarification. You don't "must" use postInit and you absolutely can use preInit function to initialise addPublicVariableEventHandler if you start scheduled script from it.
// -no comment defined- 0 = 0 spawn { "someVar" addPublicVariableEventHandler { // yourcode }; };
Posted on November 23, 2015 - 21:36 (UTC)
SilentSpike
While it is true that the event handler will only fire on the machine receiving the broadcast value. Please note that this machine can actually be the same machine broadcasting it in the cases of publicVariableClient and publicVariableServer. Examples: if (isServer) then { "OnServer" addPublicVariableEventHandler { hint "This event handler still fired!"; }; publicVariableServer "OnServer"; }; // This example assumes the client knows their own client ID // It does also work on the server (when the server ID is used) irrespective of the command name "OnClient" addPublicVariableEventHandler { hint "This event handler still fired!"; }; <client ID> publicVariableClient "OnClient";