![]() |
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:
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:
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. |
Leave me alone. >:O
|
Quote:
|
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
|
Quote:
Quote:
PHP Code:
Quote:
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). |
Wondering how often you really need to know if something is unset (I don't think I ever have)?
|
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.
|
Quote:
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.