ERROR MESSAGE:
Error
projectile.lua:14: bad argument #1 to 'pairs' (table expected, got nil)
Traceback
[C]: in function 'pairs'
projectile.lua:14: in function 'update'
[C]: in function 'xpcall'
INTENTION:
Trying to make a script that moves a projectile across the screen when I hit 'r' on my keyboard
PROGRAM CODE:
function love.load()
pressed = false
held = false
radius = 20
projectile = {}
speed = 50
end
function love.update(dt)
for k,v in pairs(projectile) do
projectile[k].x = projectile[k].x + (speed * dt)
end
inputManager()
end
function love.draw()
for k, v in pairs(projectile) do
love.graphics.circle("fill", v.x, v.y, radius)
end
end
function inputManager()
if love.keyboard.isDown('r') and held == false then
pressed = true
held = true
table.insert(projectile, {x = 50, y = 50})
end
end
function love.keyreleased(key, scancode)
if key == 'r' then
pressed = false
held = false
end
end
Require Help! - bad argument to pairs (table expected, got nil)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Require Help! - bad argument to pairs (table expected, got nil)
I'm guessing you are scanning the list of projectiles when there is none.
Change this:
to this:
You'll want to do the same in love.draw.
Change this:
Code: Select all
function love.update(dt)
for k,v in pairs(projectile) do
projectile[k].x = projectile[k].x + (speed * dt)
end
Code: Select all
function love.update(dt)
if #projectile > 0 then
for k,v in pairs(projectile) do
projectile[k].x = projectile[k].x + (speed * dt)
end
end
end
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Re: Require Help! - bad argument to pairs (table expected, got nil)
Code: Select all
function love.load()
projectiles = {}
width, height = love.graphics.getDimensions( )
for i = 1, 100 do
local projectile = {}
projectile.x = math.random(width)
projectile.y = math.random(height)
projectile.vx = math.random(200)-100
projectile.vy = math.random(200)-100
table.insert(projectiles, projectile)
end
end
Code: Select all
function love.update(dt)
-- https://love2d.org/wiki/love.keyboard.isDown
if love.keyboard.isDown("r") then
for i, projectile in pairs (projectiles) do
-- vx and vy are horizontal and vertical velocities
projectile.x = (projectile.x + dt*projectile.vx)%width
projectile.y = (projectile.y + dt*projectile.vy)%height
end
end
end
Code: Select all
function love.draw()
love.graphics.setColor(1,1,1)
for i, projectile in pairs (projectiles) do
love.graphics.circle('fill', projectile.x, projectile.y, 5)
end
end
- Attachments
-
- projectiles-01.love
- (621 Bytes) Downloaded 197 times
Re: Require Help! - bad argument to pairs (table expected, got nil)
Thank you!togFox wrote: ↑Mon Apr 26, 2021 11:10 am I'm guessing you are scanning the list of projectiles when there is none.
Change this:
to this:Code: Select all
function love.update(dt) for k,v in pairs(projectile) do projectile[k].x = projectile[k].x + (speed * dt) end
You'll want to do the same in love.draw.Code: Select all
function love.update(dt) if #projectile > 0 then for k,v in pairs(projectile) do projectile[k].x = projectile[k].x + (speed * dt) end end end
Re: Require Help! - bad argument to pairs (table expected, got nil)
Huh. How about that. After receiving so much advice on this forum I finally get to pay back.
Glad it helped.
Glad it helped.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Who is online
Users browsing this forum: Google [Bot] and 5 guests