Jump to content
Sign in to follow this  
alleycat

Test <null> ?

Recommended Posts

How to test if a variable is <null>?

I want to display a variable that might not exist for certain units. It reports <null> when displaying.

Share this post


Link to post
Share on other sites

I don't really understand what you are trying to do but to test if a variable is null:

if (_variable == null) then     //alternatively if (_variable == 0) then
      {
           //magic
      }

Share this post


Link to post
Share on other sites

Use:

isNull

eg:

if not (isNull _var) then {hint format ["%1 is not null",_var]};

Share this post


Link to post
Share on other sites
Use:

isNull

eg:

if not (isNull _var) then {hint format ["%1 is not null",_var]};

if not (isNull _aff) then {hint format ["%1 is not null",_aff]};
if  (isNull _aff) then {hint format ["%1 is  null",_aff]};

Code before and after this does run.

This never triggers.

Perhaps I explained what I want to do wrong:

I want to display a variable that might exist. If it does not exist (was not defined for the unit) then it returns literally <null>. I would like to test that.

Share this post


Link to post
Share on other sites

I think you're looking for isNil.

if (isNil "_aff") then {

};

Share this post


Link to post
Share on other sites

Undefined variable - Check with isNil "varName". Variable defined but points at no object - Check with isNull varName. Noticee the difference in quotes.

call compile "blah"; //Undefined variable error
if (isNil "blah") then {hint "Blah is nil"} else {hint "Blah is not nil"}; // blah is nil here as it is undefined
if (blah) then
{
// This code will never be executed as blah is an undefined variable. No error is given.
}
else
{
// This code will never be executed as blah is an undefined variable. No error is given. Yes, both the "then" and the "else" get ignored.
}
blah = "Jeep" createVehicle (position player);
if (isNull "blah") then {diag_log "blah is null"} else {diag_log "blah is not null"}; // will log "blah is not null" as it points at a valid vehicle.
deleteVehicle blah;
if (isNull "blah") then {diag_log "blah is null"} else {diag_log "blah is not null"}; // will log "blah is null" as the vehicle have been deleted.
if (isNil "blah") then {hint "Blah is nil"} else {hint "Blah is not nil"}; // blah is not nil here as it is undefined, it simply doesn't point at any valid object

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×