how do i check if a value is nil?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
fixylol
Prole
Posts: 21
Joined: Thu Feb 25, 2016 2:16 am

how do i check if a value is nil?

Post by fixylol »

i tried "if not [value] then" and "if [value] == nil then" but it says its trying to index a nil value :/
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: how do i check if a value is nil?

Post by bobbyjones »

Make sure to check that the table exists.

Code: Select all

If table then 
    If table[index] then
        Do stuff
    end
end
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: how do i check if a value is nil?

Post by s-ol »

Well its simpler to do

Code: Select all

if table and table.value then
end

Or 

if table and table.value == nil then
end
The problem seems to be thaz you try to do A.B but A is already nil.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
fixylol
Prole
Posts: 21
Joined: Thu Feb 25, 2016 2:16 am

Re: how do i check if a value is nil?

Post by fixylol »

the table does exist, however

Code: Select all

if not table[value] then...
still causes an error.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: how do i check if a value is nil?

Post by T-Bone »

The error message states that the table does not exist.

If the table truly does exist, then there's no way we can help you with the information you have provided.
fixylol
Prole
Posts: 21
Joined: Thu Feb 25, 2016 2:16 am

Re: how do i check if a value is nil?

Post by fixylol »

whenever i put a for loop in LoadLevel (so the map resets when the player dies), it seems to crash, but when i put a line of code that points to the level, it seems to work fine :/
Attachments
awedd-beta.love
(6.1 MiB) Downloaded 166 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: how do i check if a value is nil?

Post by Robin »

Right, you're nearly there. The problem is that you have a doubly nested table. This should work:

Code: Select all

if not CurrentLevel[chartiley] or not CurrentLevel[chartiley][chartilex] then return end
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests