publicVariable: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "[[Category:Scripting_Commands_Take_On_Helicopters" to "[[Category:Scripting Commands Take On Helicopters")
(Fix description and clean comments)
Line 11: Line 11:
____________________________________________________________________________________________
____________________________________________________________________________________________


| Broadcasts [[missionNamespace]] variable and its value to all computers.
| Reliably broadcasts [[missionNamespace]] variable and its '''current''' value to all computers (server/client).<br>
The data is sent consequently and reliably to all clients.
Variables broadcast with [[publicVariable]] during a mission will be available to [[Join In Progress|JIP]] clients with the value they held at the time.<br>
Using [[publicVariable]] too frequently in a given period of time can cause other parts of the game to experience bandwidth problems.<br><br>
Such variables are persistent and sent to the JIP client before the first batch of client-side [[Event Scripts]] (such as [[init.sqf]]) is run.
Variables broadcast with [[publicVariable]] during a mission stay persistent for [[Join In Progress|JIP]] clients.
{{Important | Using [[publicVariable]] too frequently and/or with a lot of data can cause other aspects of the game to experience bandwidth problems.}}
Such persistent variables are synced to the JIP client before the first batch of client side [[Event Scripts]] are run.<br><br>
The following data [[:Category:Types|types]] are supported:
* [[Number]]
''Since OFP version 1.34'':
* [[Boolean]]
* [[Object]]
* [[Group]]
Since ArmA version 1.00:
* [[String]]
* [[Text]]
''Since ArmA version 1.09'':
* [[Array]]  
* [[Code]]
''Since Arma 3 version 1.26'':
* [[Nothing]] ([[nil]])
<br>
<br>
''Limitations'': Cannot use reserved names, such as "[[player]]" or "[[west]]" or "[[side]]", etc.
The following [[Data Types]] are supported:
It is also not possible to transfer references to entities which are local, like [[Script|scripts]], [[Display|displays]], or local objects.
{{{!}} class{{=}}"wikitable" style{{=}}"text-align: center"
[[Team Member]] is also not supported. |Description=
! Type
{{!}} [[Number]]
{{!}} [[Boolean]]
{{!}} [[Object]]
{{!}} [[Group]]
{{!}} [[String]]
{{!}} [[Text]]
{{!}} [[Array]]
{{!}} [[Code]]
{{!}} [[Nothing]] ([[nil]])
{{!}}-
! Since
{{!}}
{{!}} {{GVI|ofp|1.34}}
{{!}} {{GVI|ofp|1.34}}
{{!}} {{GVI|ofp|1.34}}
{{!}} {{GVI|arma|1.00}}
{{!}} {{GVI|arma|1.00}}
{{!}} {{GVI|arma|1.09}}
{{!}} {{GVI|arma|1.09}}
{{!}} {{GVI|arma3|1.26}}
{{!}}}
 
{{Important| It is not possible (and illogical) to transfer a local entity reference, such as [[Script|scripts]], [[Display|displays]] or [[createVehicleLocal|local objects]].<br>
Also, note that [[Team Member]] is not supported.}} |Description=
____________________________________________________________________________________________
____________________________________________________________________________________________


| [[publicVariable]] varName |Syntax=
| [[publicVariable]] varName |Syntax=


|p1= varName: [[String]] |Parameter 1=
|p1= varName: [[String]] - the [[Variables#Global Scope|global variable]]'s [[Identifier]] |Parameter 1=


| [[Nothing]] |Return value=
| [[Nothing]] |Return value=
____________________________________________________________________________________________
____________________________________________________________________________________________
 
|x1= <code>[[publicVariable]] "CTFscoreOne";</code> |Example 1=


|x2= <code>MyPubVar = [123, "456", [[true]]];
|x1= <code>TAG_MyPublicVariable = 0;
[[publicVariable]] "MyPubVar";</code> |Example 2=
TAG_MyPublicVariable = 1;
[[publicVariable]] "TAG_MyPublicVariable"; {{cc|other clients will receive the "TAG_MyPublicVariable" variable with a '''1''' value}}
 
TAG_MyPublicVariable = 2; {{cc|needs to be broadcast again - synchronisation is not automatic}}</code> |Example 1=
 
|x2= JIP example:
<code>[[if]] ([[isNil]] "TAG_CurrentTarget") [[then]] {{cc|has the variable already been set and broadcast?}}
{
TAG_CurrentTarget = [[objNull]]; {{cc|if not, set it on the local machine}}
};
 
[[player]] [[doTarget]] TAG_CurrentTarget;</code> |Example 2=
____________________________________________________________________________________________
____________________________________________________________________________________________


| [[Multiplayer Scripting]], [[Initialization Order]], [[addPublicVariableEventHandler]], [[publicVariableClient]], [[publicVariableServer]] |See also=
| [[Multiplayer Scripting]], [[Initialization Order]], [[addPublicVariableEventHandler]], [[publicVariableClient]], [[publicVariableServer]], [[Variables#Global Scope|Global Variable]], [[Event Scripts]], [[Join In Progress]] |See also=
}}
}}


Line 55: Line 73:
<dl class="command_description">
<dl class="command_description">
<!-- Note Section BEGIN -->
<!-- Note Section BEGIN -->
<dd class="notedate">Posted on 3 Aug, 2006 23:03
<dt class="note">[[User:Hoz|Hoz]]
<dd class="note">This command broadcasts a variable to all clients, but as soon as you change the variable again, you have to use [[publicVariable]] again, as it does not automatically synchronise it.


<dd class="notedate">Posted on 12 April, 2008
<dd class="notedate">Posted on 12 April, 2008
Line 64: Line 78:
<dd class="note">Be sure to place your variable name in quotation marks.
<dd class="note">Be sure to place your variable name in quotation marks.
This may sound awfully simple, but many times I have forgotten to do this, and it has resulted in no end of headaches for me.
This may sound awfully simple, but many times I have forgotten to do this, and it has resulted in no end of headaches for me.
<dd class="notedate">Posted on 2 Feb, 2008
<dt class="note">[[User:Dr_Eyeball|Dr_Eyeball]]
<dd class="note">
When initialising a public variable to handle '''JIP''', you will usually first want to check if the public variable has already been (broadcast, received and) set locally. Otherwise you may inadvertantly overwrite the broadcast value with your default value.<br>
<br>
To perform this check, use code similar to the following to first check that the variable is nil:
<code>if ([[isNil]] "PV_abc") then
{
{{codecomment|// set the nil variable with a default value for server and both JIP & 'join at mission start'}}
PV_abc = [7, 8, 9];
};
{{codecomment|// else public variable has already been set due to a public variable broadcast.}}</code>
<br>
<dd class="notedate">Posted on 14 July, 2011
<dt class="note">[[User:kju|kju]]<dd class="note">
To make Dr_Eyeball's note even more clear:
For JIP players pV'ed variables are received and set '''BEFORE''' the init.sqf.
So to avoid the received variables getting overwritten by variable initialization normally done in the init.sqf, you '''HAVE TO''' to use the 'if ([[isNil]] "PV_abc")' pattern.
It says literally: "Only initialize value it has not yet been set. And in a JIP this may already been the cause due to publicVariable use".
<dd class="notedate">Posted on 21 September, 2013
<dt class="note">[[User:Killzone_Kid|Killzone_Kid]]<dd class="note">
To make Dr_Eyeball's and kju's notes even more clear, public variable is persistent. Once it has been broadcast it will be delivered to all clients, present and future. Therefore it is wise to check if the variable already exists on a client due to it being persistent before initialising its value.
<dd class="notedate">Posted on 23 February, 2014
<dt class="note">[[User:MulleDK13|MulleDK13=]]<dd class="note">
Just to clarify, when players JIP, they get the value of the variable from the last call to publicVariable, not the current value of the variable. Eg. with
<code>SomeVar = 5; [[publicVariable]] "SomeVar"; SomeVar = 10;</code>
Connecting players will receive 5, not the current value of 10.


<!-- Note Section END -->
<!-- Note Section END -->
Line 100: Line 83:


<h3 style="display:none">Bottom Section</h3>
<h3 style="display:none">Bottom Section</h3>
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.46|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.46|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.96|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.96|{{uc:{{PAGENAME}}}}]]
Line 106: Line 88:
[[Category:Scripting Commands ArmA|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Take On Helicopters|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Take On Helicopters|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Command Group: Multiplayer|{{uc:{{PAGENAME}}}}]]
[[Category:Command_Group:_Multiplayer|{{uc:{{PAGENAME}}}}]]
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on October 19, 2014 - 00:21 (UTC)</dd>
<dt class="note">[[User:DreadedEntity|DreadedEntity]]</dt>
<dd class="note">
I'm pretty far down the notes list so I hope this doesn't get buried.<br><br>
 
For clarity, after a variable has been publicVariable'd, scripts in all clients can use it as if it had been defined locally.<br>
 
Variables that have been publicVariable'd do not need to be pre-defined on receiving clients. In fact, this could cause issues with JIP players overwriting a publicVariable value, as mentioned above. '''(tested in A3 1.32.127785)'''<br>
Local variables cannot be publicVariable'd. '''(tested in A3 1.32.127785)''' (tested by having dedicated server publicVariable a local variable, then trying to hint it on client. Error was "Undefined variable in expression")
</dd>
</dl>
<!-- DISCONTINUE Notes -->

Revision as of 18:52, 16 May 2020

Hover & click on the images for description

Description

Description:
Reliably broadcasts missionNamespace variable and its current value to all computers (server/client).
Variables broadcast with publicVariable during a mission will be available to JIP clients with the value they held at the time.
Such variables are persistent and sent to the JIP client before the first batch of client-side Event Scripts (such as init.sqf) is run.
Using publicVariable too frequently and/or with a lot of data can cause other aspects of the game to experience bandwidth problems.


The following Data Types are supported:

Type Number Boolean Object Group String Text Array Code Nothing (nil)
Since Logo A0.png1.34 Logo A0.png1.34 Logo A0.png1.34 -wrong parameter ("Arma") defined!-1.00 -wrong parameter ("Arma") defined!-1.00 -wrong parameter ("Arma") defined!-1.09 -wrong parameter ("Arma") defined!-1.09 Arma 3 logo black.png1.26
It is not possible (and illogical) to transfer a local entity reference, such as scripts, displays or local objects.
Also, note that Team Member is not supported.
Groups:
Uncategorised

Syntax

Syntax:
publicVariable varName
Parameters:
varName: String - the global variable's Identifier
Return Value:
Nothing

Examples

Example 1:
TAG_MyPublicVariable = 0; TAG_MyPublicVariable = 1; publicVariable "TAG_MyPublicVariable"; // other clients will receive the "TAG_MyPublicVariable" variable with a 1 value TAG_MyPublicVariable = 2; // needs to be broadcast again - synchronisation is not automatic
Example 2:
JIP example: if (isNil "TAG_CurrentTarget") then // has the variable already been set and broadcast? { TAG_CurrentTarget = objNull; // if not, set it on the local machine }; player doTarget TAG_CurrentTarget;

Additional Information

See also:
Multiplayer ScriptingInitialization OrderaddPublicVariableEventHandlerpublicVariableClientpublicVariableServerGlobal VariableEvent ScriptsJoin In Progress

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

Notes

Posted on 12 April, 2008
Icemotoboy
Be sure to place your variable name in quotation marks. This may sound awfully simple, but many times I have forgotten to do this, and it has resulted in no end of headaches for me.

Bottom Section