A variable is a "storage container" or "named placeholder" for data. You can read and modify the data once this container is created.
The following links guide to the basics to understand this article:
The first thing to do to create a variable is to find its name, also called identifier; this name must be speaking to the reader. Once you find a proper name, you have to tell the game engine that you are going to use this identifier. This is called initialization or declaration. This is how it is done:
myVariable = 0;
Once created, variables will take up space in the computer's memory. This is not drastic for small variables, but if a big number of very large variables is used, some unneeded variables should be undefined in order to free up memory. This can be done by setting their value to nil.
hugeVariable = nil;
This effectively destroys a variable as if it had never existed.
Variables are only visible in certain scopes of the game. This prevents name conflicts between different variables in different scripts.
There are three scopes:
- A variable is only visible in the script, or function, or Control Structures in which it was defined.
- A variable is visible on the whole computer where it was defined.
- A variable is broadcasted over the network and visible on all computers connected to the network.
_myLocalVariable = 0;
In functions you should additionally mark variables as private using the command private. Otherwise you may modify local variables of the calling script that are visible in the function.
private _myLocalVariable = 0;
private "_myLocalVariable"; private ["_myLocalVariable1", "_myLocalVariable2", ...];
Global variables are visible on the whole computer where they are defined. Names given to units in the Mission Editor are also global variables pointing to those units, which may not be redefined or modified.
myGlobalVariable = 0;
Public variables are global variables, visible on several computers in the network. You can never have true public variables, but you can emulate their behaviour.
The value of a global variable gets broadcasted over the network using publicVariable. After the call of this command the variable will have the same value on all clients. Once you modify the variable though you have to broadcast it manually again with publicVariable.
More informations relative to private variable
If a private variable is initialized within a Control Structures (i.e. if, for, switch, while) its scope will stay within this structure (i.e. outside of the structure it will still be seen as undefined).
private _living = false;
if (alive player) then
_living = true;
hint format ["%1", _living];
Returns true since the variable was initialized before the if..then statement.
To initialize private variables so that they are available throughout the whole script (including any control structures), initialize it via the private command (e.g.
private _varname;) at the beginning of the script.
Read the article Data Types for more information about variable types.
Storing vars and functions into global vars without securing them with compileFinal (only after Arma-3) is a very bad practice in MP. Basically, if you have a script installed on a server in mission file that uses functions stored in unprotected variables then hackers can overwrite that function attached to a public variable and make it execute code for everyone and do a lot of nasty stuff. So please put all of the code for each function in a SEPARATE file and initialize them like this:
myGlobalVarFunction = compileFinal preprocessFileLineNumbers "Dir\where\function\is\fnc_globalVarFunction.sqf";
That way, the function can be called and spawned without it being subject to hackers that attempt to overwrite the global variable "myGlobalVarFunction".
Everyone happy :)