Page 1 of 1

Checking for a Nil value from a table.

Posted: Fri Jan 18, 2013 2:56 pm
by Luska72
Yesterday I made this post: viewtopic.php?f=3&t=12376
In said post, I asked about Nil vs False, and decided to make air a nil value (in my table) to save memory. I did this without any problems.
Unfortunately, I am not able to check if a certain value from my table is nil or not! I have tried the following bits of code, and can't get them to work.

Code: Select all

 if level[y][x] ~= nil then 

Code: Select all

 if not level[y][x] == nil then 

Code: Select all

 item = level[y][x]
if item ~= nil then
 

Code: Select all

 item = level[y][x]
if not item == nil then
 
You get the picture, I have also tried doing some things I googled such as using Next, but I couldn't find a solution to my problem.

If anyone can give me code or tell my where I might find code that could help me, I would be very grateful.

Thank you very much
--Luska72

PS: I was able to check if a variable is Nil without problems, so I think Lua works like this:

Code: Select all

 If VARIABLE == NIL
Lua directly compares the variable to nil

Code: Select all

 If TABLE[Y][X] == NIL 
Lua first grabs the value from the table, then compares to nil.
However, the program crashes when grabbing the value from the table, so we can't compare to nil...

Re: Checking for a Nil value from a table.

Posted: Fri Jan 18, 2013 3:08 pm
by norubal
It works for me.. strange. :|

Re: Checking for a Nil value from a table.

Posted: Fri Jan 18, 2013 3:12 pm
by micha
When doing this,

Code: Select all

if level[y][x] ~= nil then 
or one of the others, it means that level[y] has to exist. If itself is nil as well, then accessing level[y][x] is not possible.
So you need to write

Code: Select all

if level[y] ~=nil and level[y][x]~= nil then
Lua does shortcut evaluation, that means that if the first one is false, then the second one is not checked. So this should work.

Then you can save some typing. The value nil is treated as false so you can simply write

Code: Select all

if a then
instead of

Code: Select all

if a~= nil then
The code then becomes

Code: Select all

if level[y]  and level[y][x] then

Re: Checking for a Nil value from a table.

Posted: Fri Jan 18, 2013 3:21 pm
by Luska72
Thank you so much, the "y" not existing was the problem. I was able to fix it by declaring my table like this:

Code: Select all

level = {
{ }, -- # of { }s are equal to the map height
{ },
{ },
{ },
{ },
{ },
{ },
{ },
{ },
{ },
{ },
{ }
}
Thanks again, It's always annoying to make a silly mistake like that

Re: Checking for a Nil value from a table.

Posted: Sat Jan 19, 2013 10:52 am
by IndieKid
Maybe this can help. It looks for variable in array:

Code: Select all

inli.tblchk = function(tbl, chk)
    for key, value in pairs(tbl) do
      if value == chk then
        return true --this is what it returns if it found variable "chk" in array "tbl"
      end
    end
    return false  --returns "false" if it didn't find variable "chk" in array "tbl"
end
You can make it return nil.