Namespace: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "{{SideTOC}}" to "{{TOC|side}}")
m (Some wiki formatting)
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{TOC|side}}
{{TOC|side}}
{{quote|a namespace is a set of symbols that are used to organize objects of various kinds, so that these objects may be referred to by name. A namespace ensures that all the identifiers within it have unique names so that they can be easily identified.|Wikipedia|https://en.wikipedia.org/wiki/Namespace}}
{{GVI|arma2|1.00}}


Namespace - set of variables. Introduced with {{GVI|arma2|1.00|categorise}}.
A [[Namespace]] is a container for [[Variables]].
Any [[Variables#Scopes|global]] variable is always part of exactly one namespace - another namespace may contain a variable with the same [[Identifier]], but they remain two distinct independent variables.
Scripts and functions are always executed in the context of some namespace.


The default namespace is the [[missionNamespace]].


== Get namespace ==


These commands return the "Namespace" type:
== Namespaces ==
 
{| class="wikitable"
{| class="wikitable"
! Game !! Command !! Description
! Game
! Namespace
! Description
! Lifetime
|-
|-
| {{GVI|arma2|1.00}} || [[missionNamespace]] || returns the global namespace attached to the mission
| {{GVI|arma2|1.00}}
| [[missionNamespace]]
| The global namespace of the mission. Every mission has its own [[missionNamespace]].
| style="background-color: lightgreen" | Mission duration
|-
|-
| {{GVI|arma2|1.00}} || [[parsingNamespace]] || returns the global namespace attached to the [[PreProcessor Commands|Config Parser]]
| {{GVI|arma2|1.00}}
| [[parsingNamespace]]
| The global namespace attached to the [[PreProcessor Commands|Config Parser]].
| style="background-color: lightyellow" | Game session
|-
|-
| {{GVI|arma2|1.00}} || [[uiNamespace]]     || returns the global namespace attached to the user interface
| {{GVI|arma2|1.00}}
| [[uiNamespace]]
| The global namespace attached to the user interface.
| style="background-color: lightyellow" | Game session
|-
|-
| {{GVI|tkoh|1.00}} || [[profileNamespace]] || returns the global namespace attached to the user profile
| {{GVI|tkoh|1.00}}
| [[profileNamespace]]
| The global namespace attached to the user profile.
| style="background-color: lightskyblue" | [[Profile]] lifetime
|-
|-
| {{GVI|arma3|1.48}} || [[currentNamespace]] || returns the current namespace. Can be compared with [[isEqualTo]] or [[==]] and [[!=]] (since Arma 3 v1.99.146509).
| {{GVI|arma3|2.00}}
| [[localNamespace]]
| This namespace is like the [[missionNamespace]], except that its variables can not be sent over network (e.g. with [[publicVariable]]) or serialised.
| style="background-color: lightgreen" | Mission duration
|-
|-
| {{GVI|arma3|2.00}} || [[localNamespace]] || returns the local namespace.
| {{GVI|arma3|2.06}}
| [[serverNamespace]]
| The namespace used by [[ArmA: Armed Assault: Server Side Scripting|Server Event Handlers]]. This namespace is only available on servers.
| style="background-color: lightyellow" | Game session
|-
| {{GVI|arma3|2.10}}
| [[missionProfileNamespace]]
| This namespace is like [[profileNamespace]] but is also attached to a [[missionName]] or [[Description.ext#missionGroup|missionGroup]]. Automatically loaded before [[Event Scripts#init.sqf|init.sqf]].
| style="background-color: lightskyblue" | [[Profile]] lifetime
|}
|}


You can save values in this different Namespaces without overwriting the variables with the same name.
== Get namespace data ==
You can use the above with [[getVariable]] to get data.
== Set namespace data ==
To set data to a namespace, there're two methods to do:
<code>'''namespace''' [[setVariable]] ["variableName", ''value''];</code>
<code>[[with]] '''namespace''' do {
variableName = ''value'';
};</code>
[[missionNamespace]] is the default global namespace, so just "variableName = ''value'';" will store data to [[missionNamespace]].
== Namespace Lifetimes ==
=== missionNamespace ===
Mission namespace is the main default partition that exists for the duration of a mission. All variables defined in mission namespace will cease to exist when mission ends. To make variables lasting longer than a mission, use different namespaces.
=== uiNamespace and parsingNamespace ===
These are 2 different namespaces, which have similar scope. Variables defined in these namespaces exist for the duration of the game .exe running and are lost when the game is shut down.
=== profileNamespace ===


This namespace will hold variables for a very long time and is attached to the user profile. Different profiles will have own set of variables stored with them. The saving of the profile namespace is forced with [[saveProfileNamespace]] command, but in Arma 3 this command is executed by vanilla game scripts already, so saving is done automatically.
== Serialisation ==


=== localNamespace ===
Only the [[missionNamespace]] is serialised, as in only the variables from this namespace are saved when the game is saved (e.g. with [[saveGame]]) and loaded when the game is loaded (e.g. with [[loadGame]]).
This is also the reason the game will complain that one should use [[disableSerialization]] when storing UI variables in [[missionNamespace]] - UI elements are temporary and therefore exempt from serialisation by default.


Available since Arma 3 v1.99.146509. Same lifetime as [[missionNamespace]] but variables cannot be broadcast or serialized.


== Namespace serialization ==
== Scripting ==


Only [[missionNamespace]] is serialized. This means that all variables in this namespace will be saved when the game is saved and loaded when the game is loaded. This is also the reason the game will complain that one should use [[disableSerialization]] when storing UI variables in [[missionNamespace]]. UI elements are temporary and therefore are exempt from serialization by default.
Interacting with namespaces is not difficult:
* The [[getVariable]] and [[setVariable]] commands can be used to work with variables from different namespaces.
* The [[with]]-[[do]] construct can be used to switch the context of an entire code block to another namespace.
* The [[currentNamespace]] command can be used to obtain the namespace which the calling script is running in.




[[Category: Data Types]]
[[Category: Data Types]]
[[Category: Introduced with Arma 2 version 1.00]]

Latest revision as of 14:59, 26 August 2024

Logo A2.png1.00

A Namespace is a container for Variables. Any global variable is always part of exactly one namespace - another namespace may contain a variable with the same Identifier, but they remain two distinct independent variables. Scripts and functions are always executed in the context of some namespace.

The default namespace is the missionNamespace.


Namespaces

Game Namespace Description Lifetime
Logo A2.png1.00 missionNamespace The global namespace of the mission. Every mission has its own missionNamespace. Mission duration
Logo A2.png1.00 parsingNamespace The global namespace attached to the Config Parser. Game session
Logo A2.png1.00 uiNamespace The global namespace attached to the user interface. Game session
tkoh logo small.png1.00 profileNamespace The global namespace attached to the user profile. Profile lifetime
Arma 3 logo black.png2.00 localNamespace This namespace is like the missionNamespace, except that its variables can not be sent over network (e.g. with publicVariable) or serialised. Mission duration
Arma 3 logo black.png2.06 serverNamespace The namespace used by Server Event Handlers. This namespace is only available on servers. Game session
Arma 3 logo black.png2.10 missionProfileNamespace This namespace is like profileNamespace but is also attached to a missionName or missionGroup. Automatically loaded before init.sqf. Profile lifetime


Serialisation

Only the missionNamespace is serialised, as in only the variables from this namespace are saved when the game is saved (e.g. with saveGame) and loaded when the game is loaded (e.g. with loadGame). This is also the reason the game will complain that one should use disableSerialization when storing UI variables in missionNamespace - UI elements are temporary and therefore exempt from serialisation by default.


Scripting

Interacting with namespaces is not difficult:

  • The getVariable and setVariable commands can be used to work with variables from different namespaces.
  • The with-do construct can be used to switch the context of an entire code block to another namespace.
  • The currentNamespace command can be used to obtain the namespace which the calling script is running in.