Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-24-2011, 03:01 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
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:
Graal Script 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.

Graal Script 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.
Reply With Quote
  #2  
Old 01-24-2011, 03:03 AM
nullify nullify is offline
Registerd Abuser
nullify's Avatar
Join Date: May 2004
Location: The cheese state.
Posts: 851
nullify has a spectacular aura about
Leave me alone. >:O
Reply With Quote
  #3  
Old 01-24-2011, 06:53 AM
Fulg0reSama Fulg0reSama is offline
Extrinsical Anomaly
Fulg0reSama's Avatar
Join Date: Sep 2009
Location: Ohio
Posts: 3,049
Fulg0reSama has a brilliant futureFulg0reSama has a brilliant futureFulg0reSama has a brilliant futureFulg0reSama has a brilliant futureFulg0reSama has a brilliant futureFulg0reSama has a brilliant futureFulg0reSama has a brilliant futureFulg0reSama has a brilliant future
Quote:
Originally Posted by nullify View Post
Leave me alone. >:O
WhiteDragon is out to get you bro.
__________________

Careful, thoughts and opinions here scare people.
Reply With Quote
  #4  
Old 01-24-2011, 08:31 AM
MrOmega MrOmega is offline
One More Time
MrOmega's Avatar
Join Date: Aug 2010
Location: TN, USA
Posts: 631
MrOmega is an unknown quantity at this point
Send a message via AIM to MrOmega Send a message via MSN to MrOmega Send a message via Yahoo to MrOmega
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
__________________
Time is the fire in which we burn...
Up, Up, Down, Down, Left, Right, Left, Right, B, A, Select, Start! Now I got 99 LIVES!!!
Reply With Quote
  #5  
Old 01-24-2011, 09:19 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by MrOmega View Post
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 View Post
But setting actual variable to those can be confused with null, thus furthering the complication.
Yes, and this is fairly common, such as...
Graal Script 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 View Post
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).
Reply With Quote
  #6  
Old 01-24-2011, 09:42 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Wondering how often you really need to know if something is unset (I don't think I ever have)?
__________________
Reply With Quote
  #7  
Old 01-24-2011, 04:04 PM
MrOmega MrOmega is offline
One More Time
MrOmega's Avatar
Join Date: Aug 2010
Location: TN, USA
Posts: 631
MrOmega is an unknown quantity at this point
Send a message via AIM to MrOmega Send a message via MSN to MrOmega Send a message via Yahoo to MrOmega
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.
__________________
Time is the fire in which we burn...
Up, Up, Down, Down, Left, Right, Left, Right, B, A, Select, Start! Now I got 99 LIVES!!!

Last edited by MrOmega; 01-24-2011 at 04:15 PM..
Reply With Quote
  #8  
Old 01-25-2011, 09:39 AM
Chompy Chompy is offline
»\(║_o)/»
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by MrOmega View Post
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"
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().
__________________
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 03:09 AM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright (C) 1998-2008 Linux cyberjoueurs All Rights Reserved.