Home   Help Search Login Register  

Author Topic: Nil vs Null  (Read 3569 times)

0 Members and 1 Guest are viewing this topic.

Offline laggy

  • Members
  • *
  • "Behold a pale horse"
Nil vs Null
« on: 29 Jun 2009, 09:35:37 »
Hello there,

Question:
What is actually the difference of usage between Nil and Null or isNil and isNull ?

Theory:
My personal experience tells me that isNull mySoldier works better than isNil mySoldier.
Likewise isNil myVariable works better than isNull myVariable.

My experience comes from using mySoldier = objNull in a script and "objNil" doesn't exist, but I'm not sure if there are any other differences.

What is the real deal?

Thanks in advance,

Laggy
« Last Edit: 29 Jun 2009, 09:40:32 by laggy »
And I looked and beheld a pale horse and his name that sat on him was Death and Hell followed with him.

Offline i0n0s

  • Moderator
  • *****
Re: Nil vs Null
« Reply #1 on: 29 Jun 2009, 11:08:54 »
nil, isNil are for undefined variables.
Null, isNull is the "nothing found" return value, therefore the variable is set.

Offline laggy

  • Members
  • *
  • "Behold a pale horse"
Re: Nil vs Null
« Reply #2 on: 29 Jun 2009, 12:09:42 »
THX  :)

So isNil mySoldier would return false if mySoldier = objNull was set ?
« Last Edit: 29 Jun 2009, 12:12:30 by laggy »
And I looked and beheld a pale horse and his name that sat on him was Death and Hell followed with him.

Offline mikey

  • Former Staff
  • ****
  • Whitespace Whore
Re: Nil vs Null
« Reply #3 on: 29 Jun 2009, 12:33:39 »
yup, the variable exists, so it will return false

Offline laggy

  • Members
  • *
  • "Behold a pale horse"
Re: Nil vs Null
« Reply #4 on: 29 Jun 2009, 12:39:51 »
Roger  :)

Thanks both, In ArmA2 I believe they say all undefined variables will return an error. Does this mean that isNil will become obsolete?
« Last Edit: 29 Jun 2009, 12:42:30 by laggy »
And I looked and beheld a pale horse and his name that sat on him was Death and Hell followed with him.

Offline i0n0s

  • Moderator
  • *****
Re: Nil vs Null
« Reply #5 on: 29 Jun 2009, 15:28:06 »
No, A2 will warn you if you try to read from an undefined variable. But variables still can be undefined. As an example the BI function lib: You need to check if it has finished loading: This is done by reading the variable "bis_fnc_init". But the variable ain't set until it is loaded, so you need to check via isNil "bis_fnc_init" if the variable is set.

Offline laggy

  • Members
  • *
  • "Behold a pale horse"
Re: Nil vs Null
« Reply #6 on: 29 Jun 2009, 15:39:30 »
OK, thank you again  :D
And I looked and beheld a pale horse and his name that sat on him was Death and Hell followed with him.

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Nil vs Null
« Reply #7 on: 29 Jun 2009, 16:20:58 »
So isNil mySoldier would return false if mySoldier = objNull was set ?
No,
isNil "mySoldier" would return false if mySoldier = objNull was set.

Just like publicVariable, you should encase the variable name in quotes (you are checking whether the variable exists, not what the value of that variable is).
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline laggy

  • Members
  • *
  • "Behold a pale horse"
Re: Nil vs Null
« Reply #8 on: 29 Jun 2009, 17:22:57 »
Hi Spooner,

Is the quote thing something new in ArmAII or does this procedure only apply for isNil ?
I have never had to use quotes when checking for a variable that has been broadcasted by publicVariable.

Example:

action.sqf
Code: [Select]
actionGuy = _this select 0:
publicVariable "actionGuy";

Trigger:
Condition: ! isNull actionGuy
onActivation: actionGuy sidechat "I need some action!!!"

This has always worked for me in MP, so I don't understand.
Sorry if I appear stupid, it's vacation time...  :blink:

Laggy
And I looked and beheld a pale horse and his name that sat on him was Death and Hell followed with him.

Offline i0n0s

  • Moderator
  • *****
Re: Nil vs Null
« Reply #9 on: 29 Jun 2009, 17:59:03 »
From Biki:
Quote
...or whether an expression result passed as Code is undefined.

Code: [Select]
Bla = {Player sideChat "Hallo"};
isNil Bla;
Result: Hallo as sidechat, so the code gets executed...
And "isNil Bla" where Bla is undefined, results in using an undefined variable :P

Offline Trexian

  • Members
  • *
Re: Nil vs Null
« Reply #10 on: 29 Jun 2009, 18:43:21 »
So in that example, isNil would be true?  (Because nothing is returned to Bla?)

But, if you said
Code: [Select]
isNil "blech";
isNull blech;

isNil would be.... false?  because "blech" doesn't exist?  Or an error in the rpt?
isNull would be true, because "blech" hasn't been established yet.

Thanks guys, the timing on this thread is great for me to add error checking stuff like this.
« Last Edit: 29 Jun 2009, 18:47:07 by Trexian »
Sic semper tyrannosauro.

Offline i0n0s

  • Moderator
  • *****
Re: Nil vs Null
« Reply #11 on: 29 Jun 2009, 19:01:51 »
In A2:
Code: [Select]
Bla = nil;
isNil "Bla"; //returns true
isNil Bla; //returns nothing since script error
isNull Bla; //returns nothing since script error

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: Nil vs Null
« Reply #12 on: 01 Jul 2009, 19:01:05 »
It should also be noted that expressions containing a "nil value" aren't evaluated at all:

Code: [Select]
// assume "foo" wasn't initialized or set to "nil"

if foo then {
    hint "foo is true";
} else {
    hint "foo is false";
};

player sideChat "oops... no hint, no error in the RPT!";
try { return true; } finally { return false; }

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Nil vs Null
« Reply #13 on: 01 Jul 2009, 22:47:24 »
Absolutely true. However, this was not reported at all in A1, but is sometimes reported in A2 so that is a massive boon. Not sure what the exact circumstances are, but any code run directly (not execVM/spawned) from an editor field (object init or trigger activation, for example) still don't report this as an error (I understand why BIS did this, but I think they should have done things properly and not been so wussy about introducing universal error reporting on the basis that it will break lazy code  :whistle: ).
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)