Page 1 of 1

Require Help! - bad argument to pairs (table expected, got nil)

Posted: Mon Apr 26, 2021 5:31 am
by Oddnopp
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

Re: Require Help! - bad argument to pairs (table expected, got nil)

Posted: Mon Apr 26, 2021 11:10 am
by togFox
I'm guessing you are scanning the list of projectiles when there is none.

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
to this:

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
You'll want to do the same in love.draw.

Re: Require Help! - bad argument to pairs (table expected, got nil)

Posted: Mon Apr 26, 2021 1:44 pm
by darkfrei
Oddnopp wrote: Mon Apr 26, 2021 5:31 am Trying to make a script that moves a projectile across the screen when I hit 'r' on my keyboard

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

Re: Require Help! - bad argument to pairs (table expected, got nil)

Posted: Tue Apr 27, 2021 3:00 am
by Oddnopp
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:

Code: Select all

function love.update(dt)
for k,v in pairs(projectile) do
projectile[k].x = projectile[k].x + (speed * dt)
end
to this:

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
You'll want to do the same in love.draw.
Thank you!

Re: Require Help! - bad argument to pairs (table expected, got nil)

Posted: Tue Apr 27, 2021 4:42 am
by togFox
Huh. How about that. After receiving so much advice on this forum I finally get to pay back. :)

Glad it helped.