Void: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(Some wiki formatting)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
A variable of type '''Void''' is an '''undefined variable'''.
{{TOC|side}}
A [[Variables|variable]] of type '''Void''' is an '''undefined variable'''.
 


== Undefining Variables ==
== Undefining Variables ==


You can use [[nil]] to undefine variables.
[[nil]] can be used to undefine variables.


myVar = 1;
<sqf>
...
myVar = 1;
myVar = nil;
// ...
myVar = nil;
myVar => undefined
// myVar is undefined
</sqf>


== Comparisons ==


No comparison with variables of this type will work.
== String Representation ==


'''Example:'''
The [[String]] representation of an undefined variable depends on its {{Link|#Type Inference|inferred type}}:


// error
{| class="wikitable"
[[if]] (undefinedVar == ...) [[then]] ...
! [[Data Types|Data Type]]
! [[String]]
|-
| [[Anything]]
|
[[scalar bool array string 0xfcffffef]] ({{Name|ofp|short}})<br>
[[scalar bool array string 0xe0ffffef]] ({{Name|arma1|short}})
|-
| [[Nothing]]
| scalar bool array string nothing 0xfcffffef
|-
| [[Number]]
| scalar
|-
| [[Boolean]]
| bool
|-
| [[Array]]
| array
|}


// error
[[if]] (undefinedVar == undefinedVar) [[then]] ...


== Comparisons ==


No comparison with any variables will work.


In Operation Flashpoint, a Variable becomes '''undefined''' when it is assigned to an undefined value, or an expression which refers to an undefined value somewhere.
'''Example:'''
 
<sqf>
==Names==
// error
 
if (undefinedVar == definedVar) then { /* ... */ };
The [[String]] representation of an undefined variable is one of the following:
</sqf>
 
<sqf>
[[Anything]]: scalar bool array string 0xfcffffef (OFP)
// error
 
if (undefinedVar == undefinedVar) then { /* ... */ };
[[Anything]]: scalar bool array string 0xe0ffffef (ArmA)
</sqf>


[[Nothing]]: scalar bool array string nothing 0xfcffffef<br>


[[Number]]: scalar
== Detect an Undefined Variable ==


[[Boolean]]: bool
The [[isNil]] command, introduced in {{arma1}}, allows to detect if a variable exists or not:
<sqf>
HelloThere = 42;
isNil "HelloThere"; // false


[[Array]]: array
HelloThere = nil;
isNil "HelloThere"; // true
</sqf>


'''Caution: the string specification of the undefined variables is undocumented, and it is very likely to change in future version of the scripting language.'''
{{ArgTitle|3|isNil Workaround 1|{{GVI|ofp|1.00}}}}


The reliable way to check if the variable is undefined is that it silently fails any expression evaluations on it.
[[SQS Syntax]]:
<sqs>
_nil = format ["%1", _undefinedVariable]
? (format ["%1", variableToTest] == _nil) : hint "variableToTest is nil"
</sqs>
[[SQF Syntax]]:
<sqf>
_nil = format ["%1", _undefinedVariable];
if (format ["%1", variableToTest] == _nil) then { hint "variableToTest is nil" };
</sqf>


'''Example''':
{{ArgTitle|3|isNil Workaround 2|{{GVI|ofp|1.00}}}}


defined=false;if var==var then {defined=true};if var!=var then {defined=true};
{{Feature|obsolete|{{arma1}} introduced [[isNil]], and {{arma2}} forbade undefined variables usage in script - see {{Link|ArmA: Editing#Forward Compatibility}}.|arma1}}
if !defined then {/*we know it is undefined here*/}


The code above should not work in [[ArmA 2]], as stated [[ArmA:_Editing#Forward_Compatility|here]].
The alternative way to check if a variable is undefined before [[isNil]] existence is that it silently fails any expression evaluations on it:
<sqf>
defined = false;
if (var == var) then
{
defined = true;
};
if (var != var) then
{
defined = true;
};
if (!defined) then
{
// we know var is undefined here
};
</sqf>


In Armed Assault or later [[isNil]] is the preferred way to check for this, like
  '''isNil''' "varName"


==Type Inference==
== Type Inference ==


Suppose the variable '''a''' is undefined, and we set the variable '''b''' like so:<br>
Suppose the variable '''a''' is undefined, and we set the variable '''b''' like so:
b = a + []
<sqf>b = a + [];</sqf>


The inferred type of b is now array - since b was the product result of an array [[a plus b|add]] operation with a definite array ([]) and an unknown type (a)
The inferred type of b is now array - since b was the product result of an array [[+|add]] operation with a definite array ([]) and an unknown type (a)


'''b''''s string representation will now be 'array', and b wil be considered an undefined array.
'''b'''<nowiki/>'s string representation will now be "array", and b wil be considered an undefined array.


[[Category: Data Types]]


[[Category: Magic Types]]
[[Category: Magic Types]]

Latest revision as of 23:30, 15 September 2022

A variable of type Void is an undefined variable.


Undefining Variables

nil can be used to undefine variables.

myVar = 1; // ... myVar = nil; // myVar is undefined


String Representation

The String representation of an undefined variable depends on its inferred type:

Data Type String
Anything

scalar bool array string 0xfcffffef (OFP)
scalar bool array string 0xe0ffffef (ArmA)

Nothing scalar bool array string nothing 0xfcffffef
Number scalar
Boolean bool
Array array


Comparisons

No comparison with any variables will work.

Example:

// error if (undefinedVar == definedVar) then { /* ... */ };
// error if (undefinedVar == undefinedVar) then { /* ... */ };


Detect an Undefined Variable

The isNil command, introduced in Armed Assault, allows to detect if a variable exists or not:

HelloThere = 42; isNil "HelloThere"; // false HelloThere = nil; isNil "HelloThere"; // true

isNil Workaround 1

SQS Syntax:

_nil = format ["%1", _undefinedVariable] ? (format ["%1", variableToTest] == _nil) : hint "variableToTest is nil"

SQF Syntax:

_nil = format ["%1", _undefinedVariable]; if (format ["%1", variableToTest] == _nil) then { hint "variableToTest is nil" };

isNil Workaround 2

🕖
The following information is obsolete as of Armed Assault. Reason: Armed Assault introduced isNil, and Arma 2 forbade undefined variables usage in script - see ArmA: Editing - Forward Compatibility.

The alternative way to check if a variable is undefined before isNil existence is that it silently fails any expression evaluations on it:

defined = false; if (var == var) then { defined = true; }; if (var != var) then { defined = true; }; if (!defined) then { // we know var is undefined here };


Type Inference

Suppose the variable a is undefined, and we set the variable b like so:

b = a + [];

The inferred type of b is now array - since b was the product result of an array add operation with a definite array ([]) and an unknown type (a)

b's string representation will now be "array", and b wil be considered an undefined array.