Require Help! - bad argument to pairs (table expected, got 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
Oddnopp
Prole
Posts: 2
Joined: Mon Apr 26, 2021 5:24 am

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

Post 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
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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
Attachments
2021-04-26T17_18_58.png
2021-04-26T17_18_58.png (10.58 KiB) Viewed 5475 times
projectiles-01.love
(621 Bytes) Downloaded 181 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Oddnopp
Prole
Posts: 2
Joined: Mon Apr 26, 2021 5:24 am

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

Post 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!
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post by togFox »

Huh. How about that. After receiving so much advice on this forum I finally get to pay back. :)

Glad it helped.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 49 guests