Checking for a Nil value from a table.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Luska72
Prole
Posts: 18
Joined: Thu Jan 03, 2013 6:14 pm

Checking for a Nil value from a table.

Post 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...
User avatar
norubal
Party member
Posts: 137
Joined: Tue Jan 15, 2013 5:55 am

Re: Checking for a Nil value from a table.

Post by norubal »

It works for me.. strange. :|
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Checking for a Nil value from a table.

Post 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
Luska72
Prole
Posts: 18
Joined: Thu Jan 03, 2013 6:14 pm

Re: Checking for a Nil value from a table.

Post 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
User avatar
IndieKid
Citizen
Posts: 80
Joined: Sat Dec 22, 2012 7:05 pm
Contact:

Re: Checking for a Nil value from a table.

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests