setVariable: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(Page cleanup)
m (Text replacement - "<sqf>([^↵][^<]*↵[^<]*)<\/sqf>" to "<sqf> $1 </sqf>")
 
(21 intermediate revisions by 5 users not shown)
Line 20: Line 20:
|eff= local
|eff= local


|gr1= Multiplayer
|gr1= Variables


|gr2= Variables
|gr2= Namespaces


|descr= Set variable to given value in the variable space of given element. Can be used to broadcast variables over the network. To remove a variable, set it to [[nil]] (see {{HashLink|#Example 5}}).
|gr3= Multiplayer


{{Feature | Warning | [[missionNamespace]], [[uiNamespace]], [[parsingNamespace]] and [[profileNamespace]] variable names can be any string but reserved names such as script commands. For example: <tt>[[missionNamespace]] setVariable ["west", 123];</tt> will result in error:
|descr= Set variable to given value in the variable space of given element. Can be used to broadcast variables over the network.<br>
Error in expression <missionNamespace setVariable ["west", 123];>
To remove a variable, set it to [[nil]] (see {{Link|#Example 5}}) - note that this does not work on [[Object]] and [[createLocation|scripted]] [[Location]] namespaces (the variable will be set to [[nil]] but will remain listed by [[allVariables]]).
Error position: <setVariable ["west", 123];>
 
Error Reserved variable in expression
{{Feature|warning|
as [[west]] is a scripting command. Other varspaces do not have such limitation. See also [[:Category:Scripting Commands|all available script commands]].
[[missionNamespace]], [[uiNamespace]], [[parsingNamespace]] and [[profileNamespace]] variables '''cannot''' be named as commands - e.g <sqf inline>missionNamespace setVariable ["west", 123];</sqf> conflicts with the [[west]] command and will result in a {{hl|Reserved variable in expression}} error, [[west]] being a scripting command (other namespaces do not have such limitation).
See also [[:Category:Scripting Commands|all available script commands]].
}}
}}
|mp= The variable space of the object is '''local to each client''' and '''by default''' changes are not broadcast.


|s1= varspace [[setVariable]] [name, value, public]
|s1= varspace [[setVariable]] [name, value, public]


|p1= varspace: variable space in which variable can be set. Can be one of:
|p1= varspace: variable space in which variable can be set. Can be one of:
{{{!}} class="wikitable"
{{Columns|2|
! support ''public'' parameter
! local usage only (no ''public'' parameter)
{{!}}- style="vertical-align: top"
{{!}}
* [[Namespace]]
* [[Namespace]]
* [[Object]]
* [[Object]]
** {{Icon|checked}} {{GVI|arma3|2.10|size= 0.75}} {{hl|CfgAmmo}} [[Object]]s (bullets, mines etc)
** {{Icon|unchecked}} {{hl|CfgNonAIVehicles}} [[Object]]s (butterflies, proxies etc - {{hl|EmptyDetector}}s (triggers) work)
** {{Icon|unchecked}} {{hl|#local}} [[Object]]s ({{hl|#lightpoint}}, {{hl|#soundsource}} etc - see [[createVehicleLocal]])
* [[Group]]
* [[Group]]
{{!}}
* [[Location]] ({{GVI|arma3|2.10|size= 0.75}} [[createLocation|scripted ones]] only)
* [[Location]] (only the [[createLocation|scripted]] ones)
* {{GVI|arma2|1.00|size= 0.75}} [[Team Member]]
* [[Team Member]] {{Since|arma2}}
* {{GVI|arma2|1.00|size= 0.75}} [[Task]] - ''value'' will actually be set on the [[Task]]'s [[FSM]] '''if''' it exists.
* [[Task]] {{Since|arma2}}
* {{GVI|arma3|0.50|size= 0.75}} [[Control]]
** value will actually be set on [[Task]]'s [[FSM]]. If none exist, the data will not be set
* {{GVI|arma3|0.50|size= 0.75}} [[Display]]
* [[Control]] {{Since|arma3}}
}}
* [[Display]] {{Since|arma3}}
{{!}}}


|p2= name: [[String]] - variable name
|p2= name: [[String]] - variable name ([[Identifier]])


|p3= value: [[Anything]] - variable value
|p3= value: [[Anything]] - variable value


|p4= public: [[Boolean]], [[Number]] or [[Array]] of [[Number]]s - (Optional, default [[false]]): {{Since|arma2||y}}
|p4= public - (Optional, default [[false]]) can be one of:
* [[Boolean]] - when set to [[true]], the variable broadcast is global and persistent ([[Multiplayer Scripting#Join In Progress|JIP]] compatible) {{Icon|globalEffect|32}}
* [[Boolean]] - if set to [[true]], the variable is broadcast globally and is persistent ([[Multiplayer Scripting#Join In Progress|JIP]] compatible) {{Icon|globalEffect|32}}
* [[Number]] - the variable is set only on the client of given [[Multiplayer Scripting#Machine network ID|ownerId]], or if id is negative, the variable is set on every client except the given one (see {{HashLink|Multiplayer Scripting#Machine network ID}})
* [[Number]] - the variable is only set on the client with the given [[Multiplayer Scripting#Machine network ID|Machine network ID]]. If the number is negative, the variable is set on every client except for the one with the given ID.
* [[Array]] of [[Number]]s - array of [[Multiplayer Scripting#Machine network ID|ownerIds]]
* [[Array]] of [[Number]]s - array of [[Multiplayer Scripting#Machine network ID|Machine network IDs]]


Broadcast of the following [[:Category: Data Types|Data Types]] is supported:
This parameter is only available if the '''varspace''' parameter is a [[Namespace]], [[Object]] or [[Group]]. Furthermore, only the following [[:Category: Data Types|Data Types]] can be broadcast:
{{{!}} class{{=}}"wikitable" style{{=}}"text-align: center"
{{{!}} class="wikitable align-center"
! Type
! Type
{{!}} [[Number]]
{{!}} [[Number]]
Line 72: Line 68:
{{!}} [[Group]]
{{!}} [[Group]]
{{!}} [[String]]
{{!}} [[String]]
{{!}} [[Text]]
{{!}} [[Structured Text|Text]]
{{!}} [[Array]]
{{!}} [[Array]]
{{!}} [[Code]]
{{!}} [[Code]]
Line 79: Line 75:
{{!}}-
{{!}}-
! Since
! Since
{{!}} {{GVI|ofp|1.34}}
{{!}} colspan="6" {{!}} {{GVI|arma1|1.00}}
{{!}} {{GVI|ofp|1.34}}
{{!}} colspan="2" {{!}} {{GVI|arma1|1.09}}
{{!}} {{GVI|ofp|1.34}}
{{!}} {{GVI|ofp|1.34}}
{{!}} {{GVI|arma1|1.00}}
{{!}} {{GVI|arma1|1.00}}
{{!}} {{GVI|arma1|1.09}}
{{!}} {{GVI|arma1|1.09}}
{{!}} {{GVI|arma3|1.26}}
{{!}} {{GVI|arma3|1.26}}
{{!}} {{GVI|arma3|2.02}}
{{!}} {{GVI|arma3|2.02}}
{{!}}}
{{!}}}
|p4since= arma2 1.00


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


|x1= <code>_myTruck [[setVariable]] ["TAG_myPublicVariable", 123, [[true]]];</code>
|x1= <sqf>_myTruck setVariable ["TAG_myPublicVariable", 123, true];</sqf>


|x2= <code>_myTruck [[setVariable]] ["TAG_myLocalVariable", ["321", _var], [[driver]] _myTruck];</code>
|x2= <sqf>_myTruck setVariable ["TAG_myLocalVariable", ["321", _var], owner driver _myTruck];</sqf>


|x3= <code>[[missionNamespace]] [[setVariable]] ["TAG_myName", "Brian"];
|x3= <sqf>
[[hint]] TAG_myName; {{cc|hints "Brian"}}</code>
missionNamespace setVariable ["TAG_myName", "Brian"];
hint TAG_myName; // hints "Brian"
</sqf>


|x4= Get current value of a variable and if it is undefined, define it and get the defined value:
|x4= Get the current value of a variable or, if it is undefined, define it and then get the value:
<code>[[private]] _var = [[missionNamespace]] [[getVariable]] "varName";
<sqf>
[[if]] ([[isNil]] "_var") [[then]]
private _var = missionNamespace getVariable "VarName";
if (isNil "_var") then
{
{
_var = 123;
_var = 123;
[[missionNamespace]] [[setVariable]] ["varName", _var];
missionNamespace setVariable ["VarName", _var];
};
};
{{cc|_var here will contain current value of the variable varName}}</code>
// _var now contains the current value of the missionNamespace's variable varName
</sqf>


|x5= Unset <tt>_myTruck</tt>'s <tt>TAG_myPublicVariable</tt> variable globally:
|x5= Remove {{hl|TAG_myPublicVariable}} from [[missionNamespace]] (globally):
<code>_myTruck [[setVariable]] ["TAG_myPublicVariable", [[nil]], [[true]]];</code>
<sqf>missionNamespace setVariable ["TAG_myPublicVariable", nil, true];</sqf>


|seealso= [[getVariable]], [[allVariables]], [[setFSMVariable]], [[Multiplayer Scripting#Join In Progress|Multiplayer Scripting]]
|seealso= [[getVariable]] [[allVariables]] [[setFSMVariable]]
}}
}}


<dl class="command_description">
{{Note
|user= Killswitch
|timestamp= 20070818095100
|text= According to {{User|Suma}}, beginning with {{Name|arma1|short}} version 1.08:
{{Feature|quote|
[[setVariable]] now should work on any entity which can be targeted by AI, including soldier and game logic units. This includes most buildings, but not other static objects.
'''Using it with buildings one should be aware the building may be discarded because of streaming'''. In such case the variable space is lost. When used for buildings, the storage should therefore be considered non-reliable.
| {{User|Suma}}
| Talk:setVariable#setVariable local or global in Multiplayer}}
}}


<dt></dt>
{{Note
<dd class="notedate">Posted on December 9, 2015 - 20:18 (UTC)</dd>
|user= Commy2
<dt class="note">[[User:Commy2|Commy2]]</dt>
|timestamp= 20151209201800
<dd class="note">
|text= This command does not work with CfgAmmo or CfgNonAIVehicles objects, like bullets, mines or butterflies.
This command does not work with CfgAmmo or CfgNonAIVehicles objects, like bullets, mines or butterflies.
}}
</dd>


</dl>
{{Note
|user= Leopard20
|timestamp= 20220323085950
|text= Since {{GVI|arma3|2.10}} [[setVariable]] works on CfgAmmo objects (such as bullets, missiles, grenades, mines, etc) as well.
}}

Latest revision as of 19:43, 3 September 2024

Hover & click on the images for description

Description

Description:
Set variable to given value in the variable space of given element. Can be used to broadcast variables over the network.
To remove a variable, set it to nil (see Example 5) - note that this does not work on Object and scripted Location namespaces (the variable will be set to nil but will remain listed by allVariables).
missionNamespace, uiNamespace, parsingNamespace and profileNamespace variables cannot be named as commands - e.g missionNamespace setVariable ["west", 123]; conflicts with the west command and will result in a Reserved variable in expression error, west being a scripting command (other namespaces do not have such limitation). See also all available script commands.
Groups:
VariablesNamespacesMultiplayer

Syntax

Syntax:
varspace setVariable [name, value, public]
Parameters:
varspace: variable space in which variable can be set. Can be one of:
name: String - variable name (Identifier)
value: Anything - variable value
since Logo A2.png1.00
public - (Optional, default false) can be one of: This parameter is only available if the varspace parameter is a Namespace, Object or Group. Furthermore, only the following Data Types can be broadcast:
Type Number Boolean Object Group String Text Array Code Nothing (nil) HashMap
Since Logo A1 black.png1.00 Logo A1 black.png1.09 Arma 3 logo black.png1.26 Arma 3 logo black.png2.02
Return Value:
Nothing

Examples

Example 1:
_myTruck setVariable ["TAG_myPublicVariable", 123, true];
Example 2:
_myTruck setVariable ["TAG_myLocalVariable", ["321", _var], owner driver _myTruck];
Example 3:
missionNamespace setVariable ["TAG_myName", "Brian"]; hint TAG_myName; // hints "Brian"
Example 4:
Get the current value of a variable or, if it is undefined, define it and then get the value:
private _var = missionNamespace getVariable "VarName"; if (isNil "_var") then { _var = 123; missionNamespace setVariable ["VarName", _var]; }; // _var now contains the current value of the missionNamespace's variable varName
Example 5:
Remove TAG_myPublicVariable from missionNamespace (globally):
missionNamespace setVariable ["TAG_myPublicVariable", nil, true];

Additional Information

See also:
getVariable allVariables setFSMVariable

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
Killswitch - c
Posted on Aug 18, 2007 - 09:51 (UTC)
According to Suma, beginning with ArmA version 1.08:
«
« setVariable now should work on any entity which can be targeted by AI, including soldier and game logic units. This includes most buildings, but not other static objects. Using it with buildings one should be aware the building may be discarded because of streaming. In such case the variable space is lost. When used for buildings, the storage should therefore be considered non-reliable. » – Suma (source)
Commy2 - c
Posted on Dec 09, 2015 - 20:18 (UTC)
This command does not work with CfgAmmo or CfgNonAIVehicles objects, like bullets, mines or butterflies.
Leopard20 - c
Posted on Mar 23, 2022 - 08:59 (UTC)
Since Arma 3 logo black.png2.10 setVariable works on CfgAmmo objects (such as bullets, missiles, grenades, mines, etc) as well.