[HELP] attempt to perform arithmetic on field 'width'

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
zynerd
Prole
Posts: 7
Joined: Sun May 24, 2015 10:19 pm

[HELP] attempt to perform arithmetic on field 'width'

Post by zynerd »

EDIT: Solved the problem!


Hey, I started making making a game with Love2d, and when creating a function for bullet collision, I came across the following error:

enemy.lua:106: attempt to perform arithmetic on field 'width' (a nil value)

Line 106 in enemy.lua is the following:

Code: Select all

if va.x + va.width > v.x and
You can take a look at my enemy.lua and my bullet.lua below:

Code: Select all

enemy = {}

enemy.timer = 0
enemy.timerLim = math.random(3,5)
enemy.amount = math.random(1,3)
enemy.side = math.random(1,4)

function enemy.generate(dt)
	enemy.timer = enemy.timer + dt
	if enemy.timer > enemy.timerLim then
		-- SPAWN ENEMY --
		for i=1,enemy.amount do
			if enemy.side == 1 then -- LEFT
				enemy.spawn(-50,screenHeight / 2 - 25)
			end

			if enemy.side == 2 then -- TOP
				enemy.spawn(screenHeight / 2 - 25, -50)
			end

			if enemy.side == 2 then -- RIGHT
				enemy.spawn(screenWidth,screenHeight / 2 - 25)
			end

			if enemy.side == 2 then -- BOTTOM
				enemy.spawn(screenWidth / 2 - 25,screenHeight)
			end

			enemy.side = math.random(1,4)

		end

		enemy.amount = math.random(1,3)

		enemy.timerLim = math.random(3,5)
		enemy.timer = 0
	end	
end	







enemy.width = 50
enemy.height = 50
enemy.speed = 1000
enemy.friction = 10
enemyImg = love.graphics.newImage("enemy.png")

function enemy.spawn(x,y)
	table.insert(enemy, {x = x, y = y,xvel = 0,yvel = 0,health = 2,width = enemy.width,height = enemy.height})
end

function enemy.draw()
	for i,v in ipairs(enemy) do
		love.graphics.setColor(255,255,255)
		love.graphics.draw(enemyImg,v.x,v.y)
	end
end		

function enemy.physics(dt)
	for i,v in ipairs(enemy) do
		v.x = v.x + v.xvel * dt
		v.y = v.y + v.yvel * dt
		v.xvel = v.xvel * (1 - math.min(dt*enemy.friction, 1)) 
		v.yvel = v.yvel * (1 - math.min(dt*enemy.friction, 1))
	end	 
end

function enemy.AI(dt)
	for i,v in ipairs(enemy) do
		-- X AXIS --
		if player.x + player.width < v.x + v.width then
			if v.xvel > -enemy.speed then
				v.xvel = v.xvel - enemy.speed * dt
			end	
		end

		if player.x + player.width > v.x + v.width then
			if v.xvel < enemy.speed then
				v.xvel = v.xvel + enemy.speed * dt
			end	
		end

		-- Y AXIS --
		if player.y + player.height < v.y + v.height then
			if v.yvel > -enemy.speed then
				v.yvel = v.yvel - enemy.speed * dt
			end	
		end	

		if player.y + player.height > v.y + v.height then
			if v.yvel < enemy.speed then
				v.yvel = v.yvel + enemy.speed * dt
			end	
		end	

	end	
end

function enemy.bullet_collide()
	for i,v in ipairs(enemy) do
		for ia,va in ipairs(bullet) do
			if va.x + va.width > v.x and
			va.x < v.x + v.width and
			va.y + va.height > v.y and
			va.y < v.y + v.height then
				table.remove(enemy, i)
				table.remove(bullet, ia)
			end	
		end	
	end	
end	



-- PARENT FUNCTIONS --
function DRAW_ENEMY()
	enemy.draw()
end

function UPDATE_ENEMY(dt)
	enemy.physics(dt)
	enemy.AI(dt)
	enemy.generate(dt)
	enemy.bullet_collide()
end	

Code: Select all

bullet = {}
bullet.width = 5
bullet.height = 5
bullet.speed = 500

function bullet.spawn(x,y,dir)
	table.insert(bullet, {x = x, y = y, dir = dir, width = bullet.width, height = bullet.height})
end

function bullet.draw()
	for i,v in ipairs(bullet) do
		love.graphics.setColor(0,0,0)
		love.graphics.rectangle('fill',v.x,v.y,bullet.width,bullet.height)
		love.graphics.rectangle('line',v.x,v.y,bullet.width,bullet.height)
	end	
end

function bullet.move(dt)
	for i,v in ipairs(bullet) do
		if v.dir == 'up' then
			v.y = v.y - bullet.speed * dt
		end
		
		if v.dir == 'down' then
			v.y = v.y + bullet.speed * dt
		end

		if v.dir == 'right' then
			v.x = v.x + bullet.speed * dt
		end

		if v.dir == 'left' then
			v.x = v.x - bullet.speed * dt
		end	
	end	
end




-- PARENT FUNCTIONS --
function DRAW_BULLET()
	bullet.draw()
end

function UPDATE_BULLET(dt)
	bullet.move(dt)
end	
Any help would be appreciated, I'd like to continue working on my game. Thanks!
Last edited by zynerd on Sun May 24, 2015 11:30 pm, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [HELP] attempt to perform arithmetic on field 'width'

Post by Robin »

I can't find the problem right now.

Please upload a .love so that we can use our own debugging tools to look for the problem.
Help us help you: attach a .love.
zynerd
Prole
Posts: 7
Joined: Sun May 24, 2015 10:19 pm

Re: [HELP] attempt to perform arithmetic on field 'width'

Post by zynerd »

Robin wrote:I can't find the problem right now.

Please upload a .love so that we can use our own debugging tools to look for the problem.
Sorry, here ya go:

WASD to move, arrow keys to shoot.

(Forgot to mention, I'm using love 8.0)
Attachments
Game.love
(15.1 KiB) Downloaded 114 times
User avatar
I~=Spam
Party member
Posts: 206
Joined: Fri Dec 14, 2012 11:59 pm

Re: [HELP] attempt to perform arithmetic on field 'width'

Post by I~=Spam »

Are you returning "bullet" in bullet.lua?
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [HELP] attempt to perform arithmetic on field 'width'

Post by Robin »

Code: Select all

function bullet.spawn(x,y,dir)
	table.insert(bullet, {x = x, y = y, dir = dir})
end
This version doesn't assign all the properties the one in your post does. Are you somehow using two versions of your game next to each other?

Also, why are you using 0.8, BTW? 0.9 has a lot of great improvements.
Help us help you: attach a .love.
zynerd
Prole
Posts: 7
Joined: Sun May 24, 2015 10:19 pm

Re: [HELP] attempt to perform arithmetic on field 'width'

Post by zynerd »

Robin wrote:

Code: Select all

function bullet.spawn(x,y,dir)
	table.insert(bullet, {x = x, y = y, dir = dir})
end
This version doesn't assign all the properties the one in your post does. Are you somehow using two versions of your game next to each other?

Also, why are you using 0.8, BTW? 0.9 has a lot of great improvements.
Thank you so much! It turns out that I confused two of the bullet.lua files with eachother.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests