Page 1 of 1

Having a problem with a 'table[i] = nil'

Posted: Thu Jul 16, 2020 7:20 am
by Wilson_Jebette
Hello my name is Wilson, as of right now I'm having a problem with my code that I cant wrap my head around. I"m very new to love2d and I bought a class from udemy (heres the link to it: https://www.udemy.com/course/lua-love/). The problem I'm facing is where I have a for loop inside of table. Im not sure if its considering the entire function 'nil' or the draw function 'nil.


It's giving me an error output whenever the player makes contact with the zombie:

function zombies.draw()
--assigns an iterations to how many zombies spawn
for i, z in ipairs(zombies) do
z.x = z.x - math.cos(zombies.angle(z))
z.y = z.y - math.sin(zombies.angle(z))
love.graphics.draw(Sprites.zombies, z.x, z.y, zombies.angle(z), 1.5, 1.5, Sprites.zombies:getWidth()/2, Sprites.zombies:getHeight()/2)

if distanceBetween(z.x, z.y, player.x, player.y) < 100 then
for i, z in pairs(zombies) do
zombies = nil
end
end
end
end

zombie.lua:20: attempt to call global 'distanceBetween' (a nil value)

I'll leave my entire code in the description (the main problem is zombie.lua). Thank you in advance :cool:

Re: Having a problem with a 'table[i] = nil'

Posted: Thu Jul 16, 2020 12:11 pm
by sphyrth
Too many problems to tackle at once. To start things off:
#1. Where's your function distanceBetween()? The error is looking for it. You might need to require it first.
#2. zombies.draw() seems to be one of the values in your zombies table. But I can't be sure yet.
#3. Replace zombies = nil with table.remove(zombies, i).

Re: Having a problem with a 'table[i] = nil'

Posted: Thu Jul 16, 2020 6:54 pm
by pgimeno
Please also use [code]...[/code] tags to post code. Out of a code block, the [i] tag stands for italic, and your original line

Code: Select all

zombies[i] = nil
now appears as zombies = nil

and makes the rest of your post italics.