publicVariable: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
mNo edit summary
m (Some wiki formatting)
 
(70 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| ofp |= Game name
|game1= ofp
|version1= 1.34


|1.34|= Game version
|game2= ofpe
|version2= 1.00


|arg= global |= Arguments in MP
|game3= arma1
|version3= 1.00


|eff= global |= Effects in MP
|game4= arma2
____________________________________________________________________________________________
|version4= 1.00


| Broadcast variable value to all computers.
|game5= arma2oa
|version5= 1.50


Only type [[Number]] is supported in version 1.33 and before.
|game6= tkoh
|version6= 1.00


Following [[:Category:Types|Types]] are supported since version 1.34:
|game7= arma3
* [[Number]]
|version7= 0.50
* [[Boolean]]
* [[Object]]
* [[Group]]
Following [[:Category:Types|Types]] are supported since version ArmA v1.00:
* [[String]]
Since version ArmA v1.09:
Any value can be transferred, including
* [[Array]]
* [[Code]]
The only limitation is you cannot transfer references to entities which are local, like [[Script|scripts]], [[Display|displays]], or local objects.


|= Description
|gr1= Multiplayer
____________________________________________________________________________________________


| '''publicVariable''' varName |= Syntax
|gr2= Variables


|p1= varName: [[String]] |= Parameter 1
|arg= local


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


| [[addPublicVariableEventHandler]] |= See also
|descr= Reliably broadcasts [[missionNamespace]] variable and its '''current''' value to all computers (server/client).<br>
| mp= The message is sent consequently and reliably to all clients. Using publicVariable too frequently in a given period of time can cause other parts of the game to experience bandwidth problems.|=
Variables broadcast with [[publicVariable]] during a mission will be available to [[Multiplayer Scripting#Join In Progress|JIP]] clients with the value they held at the time.<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.
{{Feature|important|Using [[publicVariable]] too frequently and/or with a lot of data can cause other aspects of the game to experience bandwidth problems.}}
<br>
The following [[:Category: Data Types| Data Types]] are supported:
{{{!}} class="wikitable align-center"
! Type
{{!}} [[Number]]
{{!}} [[Boolean]]
{{!}} [[Object]]
{{!}} [[Group]]
{{!}} [[String]]
{{!}} [[Structured Text|Text]]
{{!}} [[Array]]
{{!}} [[Code]]
{{!}} [[Nothing]] ([[nil]])
{{!}} [[HashMap]]
{{!}}-
! Since
{{!}} colspan="4" {{!}} {{GVI|ofp|1.34}}
{{!}} colspan="2" {{!}} {{GVI|arma1|1.00}}
{{!}} colspan="2" {{!}} {{GVI|arma1|1.09}}
{{!}} {{GVI|arma3|1.26}}
{{!}} {{GVI|arma3|2.02}}
{{!}}}
 
{{Feature|important|
It is not possible (and illogical) to transfer a local entity reference, such as [[Script Handle|scripts]], [[Display|displays]] or [[createVehicleLocal|local objects]].<br>
Also, note that [[Team Member]] is not supported.
}}
}}


<h3 style="display:none">Notes</h3>
|s1= [[publicVariable]] varName
<dl class="command_description">
<!-- Note Section BEGIN -->


<dd class="notedate">Posted 2007</dd>
|p1= varName: [[String]] - the [[Variables#Global Scope|global variable]]'s [[Identifier]]
<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>


<dd class="notedate">Posted on 2 Feb, 2008</dd>
|r1= [[Nothing]]
<dt class="note">[[User:Dr_Eyeball|Dr_Eyeball]]</dt><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:
if ([[isNil]] "PV_abc") then
{
  // set the nil variable with a default value for server and both JIP & 'join at mission start'
  PV_abc = [7, 8, 9];
};
// else public variable has already been set due to a public variable broadcast.
<br>
</dd>
<dd class="notedate">Posted on 14 July, 2011</dd>
<dt class="note">[[User:kju|kju]]</dt><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.
|x1= <sqf>
TAG_MyPublicVariable = 0;
TAG_MyPublicVariable = 1;
publicVariable "TAG_MyPublicVariable"; // other clients will receive the "TAG_MyPublicVariable" variable with a 1 value


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.
TAG_MyPublicVariable = 2; // needs to be broadcast again - synchronisation is not automatic
</sqf>


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".
|x2= JIP example:
</dd>
<sqf>
if (isNil "TAG_CurrentTarget") then // has the variable already been set and broadcast?
{
TAG_CurrentTarget = objNull; // if not, set it on the local machine
};


<dd class="notedate">Posted on 12 April, 2008</dd>
player doTarget TAG_CurrentTarget;
<dt class="note">[[User:Icemotoboy|Icemotoboy]]</dt><dd class="note">
</sqf>
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.
</dd>


<dd class="notedate">Posted on 13 April, 2008</dd>
|x3= <sqf>
<dt class="note">[[User:corsair|=FSI=Corsair]]</dt><dd class="note">
TAG_BossName = "EvilBigBoss";
Var type "side" cannot be sent on 1.08, it will work in 1.11, 1.09 not tested
</dd>


<!-- Note Section END -->
publicVariable TAG_BossName; // wrong - will try to publicVariable "EvilBigBoss" variable, that does not exist
</dl>
publicVariable "TAG_BossName"; // correct - important, do not forget the QUOTES
</sqf>


<h3 style="display:none">Bottom Section</h3>
|seealso= [[Multiplayer Scripting]] [[Initialisation Order]] [[addPublicVariableEventHandler]] [[publicVariableClient]] [[publicVariableServer]] [[Variables#Global Scope|Global Variable]] [[Event Scripts]] [[Multiplayer Scripting#Join In Progress|Join In Progress]]
[[Category:Scripting Commands|PUBLICVARIABLE]]
}}
[[Category:Scripting Commands OFP 1.96|PUBLICVARIABLE]]
[[Category:Scripting Commands OFP 1.46|PUBLICVARIABLE]]
[[Category:Scripting Commands ArmA|PUBLICVARIABLE]]
[[Category:Command_Group:_Multiplayer|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands VBS2|{{uc:{{PAGENAME}}}}]]

Latest revision as of 15:57, 26 July 2023

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) HashMap
Since Logo A0.png1.34 Logo A1 black.png1.00 Logo A1 black.png1.09 Arma 3 logo black.png1.26 Arma 3 logo black.png2.02
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:
MultiplayerVariables

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;
Example 3:
TAG_BossName = "EvilBigBoss"; publicVariable TAG_BossName; // wrong - will try to publicVariable "EvilBigBoss" variable, that does not exist publicVariable "TAG_BossName"; // correct - important, do not forget the QUOTES

Additional Information

See also:
Multiplayer Scripting Initialisation Order addPublicVariableEventHandler publicVariableClient publicVariableServer Global Variable Event Scripts Join 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