Having a problem with a 'table[i] = 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
Wilson_Jebette
Prole
Posts: 3
Joined: Thu Jul 16, 2020 7:09 am

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

Post 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:
Attachments
main.lua
(492 Bytes) Downloaded 86 times
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

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

Post 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).
User avatar
pgimeno
Party member
Posts: 3689
Joined: Sun Oct 18, 2015 2:58 pm

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

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

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 6 guests