10-15-2019, 12:35 PM
Variables
As we've seen in the previous chapter, data can be represented in SQF as different types. To store data, we need to make sure we can retrieve it again when we need it. To easily do it, we tie data to a name. This is called a variable.
A variable is simply a named data item of a distinct data type. To define a variable, we have to assign it a type:
myVariable = 1;
This definition does several things: It declares a variable called "myVariable", gives it the value of 1, and also determines that it's data type is a number. Unlike other programming languages, the data type itself can change, though.
myVariable = "A";
This reassigns the value of myVariable to be "A", i.e. a string.
A variable can take the place of any data item. Thus
myVariable = 10;
myOtherVariable = 100;
myOtherVariable = myOtherVariable + myVariable;
assigns myOtherVariable the value 110.
Naming
Variable names can consist of characters (a - z, A- Z), numbers (0 - 9) and the underscore character ("_"). They must NOT start with a number, though. As such, these a valid names:
myVariable
MyVaRiAbLe
some_variable0
_someVariable
And these are invalid
0xxx
-ky
some-illegal-variable-name
Scope
Variables starting with an underscore character ("_") are considered local, while characters starting with a letter are considered global. A global variable is known on the current client or server anywhere, not only in the currently executed script, but also in other scripts. On the other hand, local variables are only known in the current scope. The current scope is either the current script file, the current function, or the current control structure. We'll discuss these things later. Suffice to say for now that local variables are not known outside the currently running script, while global variables are known everywhere on the current machine.
Network considerations
Variables as such are only visible on the current machine. That is, even if you define a global variable "myGlobalVariable" on your client, it will ONLY be visible on this client, not on any other client. To make it known beyond the boundaries of your current machine, you can broadcast it via the publicVariable command:
myVariable = 1; publicVariable "myVariable";
This distributes the variable among all clients and server in the current game session. Note, though, that this ONLY transfers the current state.
So, after executing the above line, myVariable will be known on any client/server and will have the value 1 on each.
Now, if you execute
myVariable = myVariable + 1;
on a client, then the variable will be 2 on that client but 1 everywhere else. This is an important point when working with networks.
Arrays
Arrays are handled somewhat differently. They are not copied and modification of one modifies the other.
For example:
array1 = ["A", "B", "C"];
array2 = array1;
array1 set [1, "D"]; // Set = array set[x, value] sets element number x to value
will make array1have the value ["A", "D", "C"], and attay2 hast the same elements. This is a so called assignment by reference in contrast to an assignment by value as in the other cases
To make a real copy, use
array2 = +array1;
To summarize:
As we've seen in the previous chapter, data can be represented in SQF as different types. To store data, we need to make sure we can retrieve it again when we need it. To easily do it, we tie data to a name. This is called a variable.
A variable is simply a named data item of a distinct data type. To define a variable, we have to assign it a type:
myVariable = 1;
This definition does several things: It declares a variable called "myVariable", gives it the value of 1, and also determines that it's data type is a number. Unlike other programming languages, the data type itself can change, though.
myVariable = "A";
This reassigns the value of myVariable to be "A", i.e. a string.
A variable can take the place of any data item. Thus
myVariable = 10;
myOtherVariable = 100;
myOtherVariable = myOtherVariable + myVariable;
assigns myOtherVariable the value 110.
Naming
Variable names can consist of characters (a - z, A- Z), numbers (0 - 9) and the underscore character ("_"). They must NOT start with a number, though. As such, these a valid names:
myVariable
MyVaRiAbLe
some_variable0
_someVariable
And these are invalid
0xxx
-ky
some-illegal-variable-name
Scope
Variables starting with an underscore character ("_") are considered local, while characters starting with a letter are considered global. A global variable is known on the current client or server anywhere, not only in the currently executed script, but also in other scripts. On the other hand, local variables are only known in the current scope. The current scope is either the current script file, the current function, or the current control structure. We'll discuss these things later. Suffice to say for now that local variables are not known outside the currently running script, while global variables are known everywhere on the current machine.
Network considerations
Variables as such are only visible on the current machine. That is, even if you define a global variable "myGlobalVariable" on your client, it will ONLY be visible on this client, not on any other client. To make it known beyond the boundaries of your current machine, you can broadcast it via the publicVariable command:
myVariable = 1; publicVariable "myVariable";
This distributes the variable among all clients and server in the current game session. Note, though, that this ONLY transfers the current state.
So, after executing the above line, myVariable will be known on any client/server and will have the value 1 on each.
Now, if you execute
myVariable = myVariable + 1;
on a client, then the variable will be 2 on that client but 1 everywhere else. This is an important point when working with networks.
Arrays
Arrays are handled somewhat differently. They are not copied and modification of one modifies the other.
For example:
array1 = ["A", "B", "C"];
array2 = array1;
array1 set [1, "D"]; // Set = array set[x, value] sets element number x to value
will make array1have the value ["A", "D", "C"], and attay2 hast the same elements. This is a so called assignment by reference in contrast to an assignment by value as in the other cases
To make a real copy, use
array2 = +array1;
To summarize:
- Variables are named containers for data types
- Variable names start with letters or the underscore character
- Global variables are known outside their current scope, private variabls can are not
- To make a variable known on other machines, use the publicVariable command
- Arrays are copied by reference, not by value, unless explicitly requested