Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Debunking null (https://forums.graalonline.com/forums/showthread.php?t=134261826)

WhiteDragon 01-24-2011 03:01 AM

Debunking null
 
One of the weirdest things about Graal is how it handles null. I have done serious amounts of testing and have finally figured out how it works.

null behaves exactly like an unset variable.

This means, if you go through your code and replace every instance of null with lolThisDoesntExist, it will work exactly the same.

HOWEVER, it is impossible to "set" null. This is the "special" thing about null.

Meaning, this code doesn't work:
PHP Code:

null true

Again, this is the only special thing about null. Otherwise, it is the same as any other unset variable.


The reason null can be confusing, is not because it is special, but because the weird behavior of unset variables.

To make this clear, I will use lolunset instead of null to show that this behavior is the same for any unset variable.

PHP Code:

echo(lolunset);
// "" : This is what happens when you echo any unset variable.

echo(@(lolunset)); 
// "" : First force it to a string, then echo it. Same as before.

echo(lolunset.type());
// "-1" : This is what gets echoed for the type of any unset variable.


temp.lolunset// set a variable to another unset variable

echo(temp.a);
// "0" : NOTICE!!!! When you set a variable to an unset variable,
// it turns into 0!

echo(@(temp.a));
// "0" : Same.

echo(temp.a.type());
// "0" : 0 being the type of a number, this confirms that temp.a
// really does equal 0.






echo(lolunset == "");
// "1" : Comparing an unset variable to a string is true

echo(@(lolunset) == "");
// "1" : Forcing the unset variable to a string then comparing it to a string
// is true.

  
echo(lolunset == 0);
// "1" : Comparing an unset variable to 0 is true.

echo(@(lolunset) == 0);
// "1" : Forcing an unset variable to a string then comparing it to 0
// is still true.

  
echo(temp.== "");
// "1" : Comparing a variable with 0 in it to a string is true.

echo(@(temp.a) == "");
// "0" : Forcing a variable with 0 in it to a string then comparing it to a
// string is FALSE. Weird as hell but it makes a difference if
// the coercion is done by @() or by == apparently.
//
// The same happens even if you temp.a = 0; directly, so it is unrelated to
// lolunset and null stuff. But worth noting.

echo(temp.== 0);
// "1" : Comparing 0 to 0, is true, obviously.
echo(@(temp.a) == 0);
// "1" : Comparing 0 in string from to 0 is true as well. 

You can replace lolunset with null in the above code, and it behaves exactly the same. The only difference between the two is that you can't overwrite null with true and screw up a whole program, so it's obviously a better idea to use null than any other unset variable.


However, due to unset variables just being... weird (as seen above), I'd recommend always comparing to 0 or "" specifically, since it's a little less weird and you only need to think about the usual casting rules.

If you for some reason really need to check if a variable is unset, I'd say the best way is using x.type() == -1.


Hope this saves someone else a headache and maybe sheds some light on the various weird dark corners of GS2.

nullify 01-24-2011 03:03 AM

Leave me alone. >:O

Fulg0reSama 01-24-2011 06:53 AM

Quote:

Originally Posted by nullify (Post 1625262)
Leave me alone. >:O

WhiteDragon is out to get you bro.

MrOmega 01-24-2011 08:31 AM

I dabbled with this a couple months ago. I found it strange though that null can equal -1 and 0. But setting actual variable to those can be confused with null, thus furthering the complication. I prefer using the '!' (Not) before a var to check to see if it is indeed null. Null is full of lulz but it can be irritating cause of the flawz. z.z

WhiteDragon 01-24-2011 09:19 AM

Quote:

Originally Posted by MrOmega (Post 1625295)
I found it strange though that null can equal -1 and 0.

null should never be -1, that was the type of null that was -1.

Quote:

Originally Posted by MrOmega (Post 1625295)
But setting actual variable to those can be confused with null, thus furthering the complication.

Yes, and this is fairly common, such as...
PHP Code:

function onCreated() {
  
temp.this.someFunction(0);
}
function 
someFunction(temp.i) {
  if (
temp.== 1) {
    return 
"yeay!";
  }


This will result in temp.a sometimes being set to an empty value, meaning it will have the same behavior as what I outlined in my post.

Quote:

Originally Posted by MrOmega (Post 1625295)
I prefer using the '!' (Not) before a var to check to see if it is indeed null. Null is full of lulz but it can be irritating cause of the flawz. z.z

Seems like a poor way to check if something is null since it would also be true for anything which can be coerced to the integer 0.
I think using x.type() == -1 is the best way to check if a variable doesn't exist (or if the return type of a function is null/non-existent).

cbk1994 01-24-2011 09:42 AM

Wondering how often you really need to know if something is unset (I don't think I ever have)?

MrOmega 01-24-2011 04:04 PM

It equals -1 (sort of) when you test for a player or npc and it comes back null, since players can have an id of 0.

Chompy 01-25-2011 09:39 AM

Quote:

Originally Posted by MrOmega (Post 1625328)
It equals -1 (sort of) when you test for a player or npc and it comes back null, since players can have an id of 0.

That's a different matter though. 0 doesn't equal -1 "sort of" :p
Because those functions returns an index it would be logical to return -1 for not found, since 0 is the index for the first member.

You'll just have to adapt and use those functions the same way as you use obj.type().


All times are GMT +2. The time now is 07:52 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.