isNil: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
m (merged my notes, deleted irrelevant information, added a new finding)
Line 106: Line 106:
<dt class="note">[[User:DreadedEntity|DreadedEntity]]</dt>
<dt class="note">[[User:DreadedEntity|DreadedEntity]]</dt>
<dd class="note">
<dd class="note">
You can also use [[isNil]] to check if an array element exists.
You can also use [[isNil]] to check if an array element exists or if a [[setVariable]] variable exists
<code>_array = [0,1,2,3];
<code>_array = [0,1,2,3];
if (isNil {_array select 4}) then {hint "Element does not exist";};</code>
if (isNil {_array select 4}) then {hint "Element does not exist";};</code>
</dd>
<code>if ([[isNil]] {[[missionNamespace]] [[getVariable]] "MY_VARIABLE"})</code>
</dl>
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.
<!-- DISCONTINUE Notes -->
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on November 1, 2014 - 07:21 (UTC)</dd>
<dt class="note">[[User:DreadedEntity|DreadedEntity]]</dt>
<dd class="note">
I forgot to mention in my last note, using the same method you can also check whether a variable has been defined using [[setVariable]].
<code>if ([[isNil]] {[[missionNamespace]] [[getVariable]] "MY_VARIABLE"}) then  //variable is nil
{[[missionNamespace]] [[setVariable]] ["MY_VARIABLE", 999];};        //variable becomes 999
 
if ([[isNil]] {[[missionNamespace]] [[getVariable]] "MY_VARIABLE"}) then //variable is 999
{[[hint]] "MY_VARIABLE is nil!";}                                //skipped
else
{[[hint]] [[str]] ([[missionNamespace]] [[getVariable]] "MY_VARIABLE");};    //hint "999"</code>
</dd>
</dd>
</dl>
</dl>
<!-- DISCONTINUE Notes -->
<!-- DISCONTINUE Notes -->

Revision as of 22:01, 15 December 2014

-wrong parameter ("Arma") defined!-1.00
Hover & click on the images for description

Description

Description:
Tests whether the variable defined by the String argument is undefined, or whether an expression result passed as Code is undefined.
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.
Groups:
Uncategorised

Syntax

Syntax:
isnil variable
Parameters:
variable: String or Code
Return Value:
Boolean

Examples

Example 1:
if (isNil "_pokus") then { _pokus = 0; };

Additional Information

See also:
nilVariables

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

Notes

ColonelSandersLite

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.
General Barron

^ 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:
_myvar = "_hisvar";
isnil _myvar;
//will return true if _hisvar is null
_myvar = {tank1};
sleep (random 50);
isnil _myvar;
//will return if tank1 is nil, at the time the isnil command is checked (not at the time _myvar is established)

--General Barron 10:37, 30 December 2009 (CET)


Igneous01

isNil is also able to check if an expression is undefined. As such, an alternative way to check variables would be:
isNil {variable}

you can use this method to also check if variables defined using setVariable exist as well:

isNil {player getVariable "Something"}

As well as testing if a function returns a value

func_ChangeVehicleName = 
{
   _this setVehicleVarName "newName";
};

if (isNil {player call func_ChangeVehicleName})    // returns true, because this function does not return anything


Bottom Section

Posted on September 25, 2014 - 09:48 (UTC)
Kenoxite
While isNil isn't available in OFP/CWA you can easily emulate it with something like this:
_nil = format["%1",_nilstring];
?(format["%1",foo]==_nil): foo = "Hello World!"
Posted on October 25, 2014 - 01:51 (UTC)
DreadedEntity
You can also use isNil to check if an array element exists or if a setVariable variable exists _array = [0,1,2,3]; if (isNil {_array select 4}) then {hint "Element does not exist";}; if (isNil {missionNamespace getVariable "MY_VARIABLE"}) 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.