isNil: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
m (2.16 -> 2.18)
 
(73 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma |= Game name
|game1= arma1
|version1= 1.00


|1.00|= Game version
|game2= arma2
____________________________________________________________________________________________
|version2= 1.00


| Tests whether the variable defined by the [[String]] argument is undefined, or whether an expression result passed as [[Code]] is undefined.
|game3= arma2oa
<br>The command returns [[true]] if the variable or the expression result is undefined (i.e. the expression result is [[Void]]), and [[false]] in all other cases. |= Description
|version3= 1.50
____________________________________________________________________________________________


| '''isNil''' variable |= Syntax
|game4= tkoh
|version4= 1.00


|p1= variable: [[String]] or [[Code]]|= Parameter 1
|game5= arma3
|version5= 0.50


| [[Boolean]] |= Return value
|gr1= Variables
____________________________________________________________________________________________


|x1 = <code>[[if]] ([[isNil]] "_pokus") [[then]] {
|descr= Tests whether a variable is [[nil]] or undefined from its [[Identifier|identifier]], or whether the given expression evaluates to [[nil]] or [[Nothing]].
    _pokus = 0;
{{Feature|arma0|This command does not exist in {{ofp}}/{{arma0}} but can be emulated (see {{Link|#Example 5}}).}}
};</code>|= Example1


|x2 = <code>[[isNil]] {[[player]] [[getVariable]] "someVar"};</code>|= Example2
|s1= [[isNil]] variableName
____________________________________________________________________________________________


| [[nil]], [[Variables]] |= See also
|p1= variableName: '''[[String]]''' - [[missionNamespace]]'s or local [[Variables|variable]]'s [[Identifier|identifier]] (e.g "globalVariable", "_localVariable")
 
}}
 
<h3 style="display:none">Notes</h3>
<dl class="command_description">
<!-- Note Section BEGIN -->


|r1= [[Boolean]] - [[true]] if variable is [[nil]] or undefined


<dt class="note">'''[[User:ColonelSandersLite|ColonelSandersLite]]'''
|s2= [[isNil]] code
<dd class="note"><br>
I recently had a strange experience with this command.  I forgot to wrap the name of the variable with quotes, and it returned the opposite of the true null status of the variable.  Just something to watch out for.


|p21= code: [[Code]] - code to evaluate:
* the code '''will''' be executed; <sqf inline>isNil { player setDamage 1 };</sqf> ''will'' kill the player
* the code will not be allowed to [[canSuspend|suspend]] while expression is evaluated, even if the parent scope allows it (see {{Link|#Example 4}}).


<dt class="note">'''[[User:General_Barron|General Barron]]'''
|r2= [[Boolean]] - [[true]] if ''code'' returns [[Nothing]], [[false]] otherwise
<dd class="note"><br>
^ If you don't wrap the name of the variable in quotes, then it will instead read the value of the variable itself. If that variable is a string or code, then the command will use that string or code held by the variable. Example:


<pre>_myvar = "_hisvar";
|s3= namespace [[isNil]] variableName
isnil _myvar;
|s3since= arma3 2.18
//will return true if _hisvar is null
|p41= namespace: [[Namespace]], [[Object]], [[Group]] or [[HashMap]]
_myvar = {tank1};
|p42= variableName: [[String]]
sleep (random 50);
|r3= [[Boolean]] - [[true]] if variable is [[nil]] or undefined
isnil _myvar;
//will return if tank1 is nil, at the time the isnil command is checked (not at the time _myvar is established)
</pre>
--[[User:General Barron|General Barron]] 10:37, 30 December 2009 (CET)


|x1= <sqf>
if (isNil "TAG_globalVariable") then { TAG_globalVariable = 0 };
if (isNil "_localVariable") then { hint "_localVariable is nil" };
</sqf>


<dt class="note">'''[[User:Igneous01|Igneous01]]'''
|x2= <sqf>isNil { player getVariable "someVar" };</sqf>
<dd class="note"><br>
isNil is also able to check if an expression is undefined. As such, an alternative way to check variables would be:
<pre>
isNil {variable}
</pre>


you can use this method to also check if variables defined using setVariable exist as well:
|x3= <sqf>
<pre>
_myArray = [0, 1];
isNil {player getVariable "Something"}
isNil { _myArray select 0 }; // returns false
</pre>
isNil { _myArray select 1 }; // returns false
isNil { _myArray select 2 }; // returns true
isNil { _myArray select 3 }; // throws a script error. only length+1 select is allowed
</sqf>


As well as testing if a function returns a value
|x4= This trick forces executing something in [[Scheduler|Unscheduled Environment]]:
<pre>
<sqf>
func_ChangeVehicleName =
0 spawn {
{
systemChat str canSuspend; // chat shows true
  _this setVehicleVarName "newName";
isNil { hint str canSuspend }; // hint shows false
};
};
</sqf>


if (isNil {player call func_ChangeVehicleName})   // returns true, because this function does not return anything
|x5= {{ofp}} [[String]] comparison workaround<br>
</pre>
[[SQS Syntax]]:
<sqs>
_nil = format ["%1", _undefinedVariable]
? (format ["%1", variableToTest] == _nil) : hint "variableToTest is nil"
</sqs>


[[SQF Syntax]]:
<!-- Note Section END -->
<sqf>
</dl>
_nil = format ["%1", _undefinedVariable];
if (format ["%1", variableToTest] == _nil) then { hint "variableToTest is nil" };
</sqf>


<h3 style="display:none">Bottom Section</h3>
|seealso= [[nil]] [[Variables]] [[Scheduler]]
[[Category:Scripting Commands|ISNIL]]
}}
[[Category:Scripting Commands ArmA|ISNIL]]
[[Category:Command_Group:_Object_Information|{{uc:{{PAGENAME}}}}]]
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting_Commands_Take_On_Helicopters|{{uc:{{PAGENAME}}}}]]
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on September 25, 2014 - 09:48 (UTC)</dd>
<dt class="note">[[User:Kenoxite|Kenoxite]]</dt>
<dd class="note">
While [[isNil]] isn't available in OFP/CWA you can easily emulate it with something like this:
<pre>
_nil = format["%1",_nilstring];
?(format["%1",foo]==_nil): foo = "Hello World!"
</pre>
</dd>
</dl>
<!-- DISCONTINUE Notes -->
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on October 25, 2014 - 01:51 (UTC)</dd>
<dt class="note">[[User:DreadedEntity|DreadedEntity]]</dt>
<dd class="note">
You can also use [[isNil]] to check if an array element exists or if a [[setVariable]] variable exists
<code>_array = [0,1,2,3];
if (isNil {_array select 4}) then {hint "Element does not exist";};</code>
<code>if ([[isNil]] {[[missionNamespace]] [[getVariable]] "MY_VARIABLE"})</code>
When trying to test array elements, you can only test elements that are 1 element out of range. Testing elements 2 or more elements out of range will result in a script error.
</dd>
</dl>
<!-- DISCONTINUE Notes -->
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on June 25, 2017 - 15:04 (UTC)</dd>
<dt class="note">[[User:IT07|IT07]]</dt>
<dd class="note">
'''isNil''' in combination with ''code'' '''will execute that code''' so be aware.
</dd>
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 16:03, 8 February 2024

Hover & click on the images for description

Description

Description:
Tests whether a variable is nil or undefined from its identifier, or whether the given expression evaluates to nil or Nothing.
Operation Flashpoint
This command does not exist in Operation Flashpoint/Arma: Cold War Assault but can be emulated (see Example 5).
Groups:
Variables

Syntax 1

Syntax:
isNil variableName
Parameters:
variableName: String - missionNamespace's or local variable's identifier (e.g "globalVariable", "_localVariable")
Return Value:
Boolean - true if variable is nil or undefined

Syntax 2

Syntax:
isNil code
Parameters:
code: Code - code to evaluate:
  • the code will be executed; isNil { player setDamage 1 }; will kill the player
  • the code will not be allowed to suspend while expression is evaluated, even if the parent scope allows it (see Example 4).
Return Value:
Boolean - true if code returns Nothing, false otherwise

Syntax 3

Syntax:
namespace isNil variableName
Parameters:
namespace: Namespace, Object, Group or HashMap
variableName: String
Return Value:
Boolean - true if variable is nil or undefined

Examples

Example 1:
if (isNil "TAG_globalVariable") then { TAG_globalVariable = 0 }; if (isNil "_localVariable") then { hint "_localVariable is nil" };
Example 2:
isNil { player getVariable "someVar" };
Example 3:
_myArray = [0, 1]; isNil { _myArray select 0 }; // returns false isNil { _myArray select 1 }; // returns false isNil { _myArray select 2 }; // returns true isNil { _myArray select 3 }; // throws a script error. only length+1 select is allowed
Example 4:
This trick forces executing something in Unscheduled Environment:
0 spawn { systemChat str canSuspend; // chat shows true isNil { hint str canSuspend }; // hint shows false };
Example 5:
Operation Flashpoint String comparison workaround
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" };

Additional Information

See also:
nil Variables Scheduler

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