Jump to content
🛡️FORUMS ARE IN READ-ONLY MODE Read more... ×
Sign in to follow this  
fabrizio_t

How to check if a variable is undefined

Recommended Posts

I need to check if a certain variable is undefined (and assign to it a default value in that case).

Please give my your advice.

Share this post


Link to post
Share on other sites

I think isNil may be what you want. Despite the text on Biki I think it tests for definedness rather than a null value (which is what isNull is for).

I'll do some checking when I can on my ArmA box...

Share this post


Link to post
Share on other sites

Things you can put in Init.sqs

score = "23" (this is a string global variable)

spore=12 (this is a numeric global variable)

"2" ObjStatus "HIDDEN"

"markername" SetMarkerType "Empty"

1 SetRadioMsg "NULL"

obj1=false

obj2=false

Hope this helps. If not good luck.

Share this post


Link to post
Share on other sites

I didn't know about isNil wow_o.gif

In OFP, it worked this way :

A non defined variable points to objNull

Therefore, you can use isNull. Do not use x==objNull, as explained ine the Biki.

What's the difference between isNull and isNil, if any?

Share this post


Link to post
Share on other sites

Can you check if 0? If so then its undefined

variable = 0 in init.

Then when variable becomes defined its value is anything other than zero.

I guess it depends on the type of variable but you should be able to reserve a specific value as undefined at init, once its defined as something then switch the value and check for it at the start of a function. I know this creates a definition but its reserved as only one thing but maybe for some reason the avariable has to be completely empty.

Share this post


Link to post
Share on other sites
Quote[/b] ]What's the difference between isNull and isNil, if any?

I just confirmed it.

Running this command

player sidechat format ["defined: %1 null: %2",not isNil ("_v"),isNull _v] ;

For _v =nil , results are "defined: FALSE null: BOOL"

For _v =grpNull , results are "defined: TRUE null: TRUE"

For _v =player , results are "defined: TRUE null: FALSE"

Share this post


Link to post
Share on other sites
What's the difference between isNull and isNil, if any?

The difference is that isNil can tell you whether the variable has been even initialized - eg. whether it exists, while isNull cannot determine whether the variable exists and is null, or it doesn't exists at all.

Consider this:<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">myVar1 = ObjNull

? isNil myVar1 : Hint "Variable 1 does not exists"

? isNull myVar1 : Hint "Variable 1 is null"

? isNil myVar2 : Hint "Variable 2 does not exists"

? isNull myVar2 : Hint "Variable 2 is null"

if you run the example above, you should get messages "Variable 1 is null", "Variable 2 does not exists", and "Variable 2 is null".

Because the variable myVar1 is initialized (it exists, and it has been assigned a value of ObjNull), the first condition will return false while the second will return true.

But both third and fourth conditions will return true because the variable myVar2 has not been initialized and doesn't exists.

I hope i didn't obscured things more, by this "explanation", than cleared them smile_o.gif

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  

×