Page 1 of 1

Reading Error Messages

Posted: Fri May 27, 2016 10:31 pm
by NatEye
So I tried to open up my game in Love but I keep getting this message:

Error

main.lua:43: attempt to compare nil with number

Traceback

main.lua:43: in function 'update'
[C]: in function 'xpcall'

Here is my code:

Code: Select all

local player
local p_x
local p_y
local p_speed
local stillhere

function love.draw()
	love.graphics.setBackgroundColor(0,0,0)
end

function love.load()
	player = love.graphics.newImage('human1.png')
	p_x = 0
	p_y = 0
	p_speed = 100
end

function love.update(dt)
	--Left/Right
	
	if love.keyboard.isDown("a") then
		p_x = p_x - p_speed * dt
		player = love.graphics.newImage('human3.png')
	end
	
	if love.keyboard.isDown("d") then
		p_x = p_x + p_speed * dt
		player = love.graphics.newImage('human1.png')
	end
	
	--Up/Down
	if love.keyboard.isDown("s") then
		p_y = p_y + p_speed * dt
		player = love.graphics.newImage('human4.png')
	end
	
	if love.keyboard.isDown("w") then
		p_y = p_y - p_speed * dt
		player = love.graphics.newImage('human2.png')
	end
	
	-- Left/Right Collision
	if p_x <0 then
		p_x = 0
	end
	
	if p_x > love.graphics.getWidth() - player:getWidth() then
	p_x = love.graphics.getWidth() - player:getWidth()
	end
	
	--Up/Down Collision
	if p_y < 0 then
		p_y = 0
	end

	if p_y > love.graphics.getHeight() - player:getHeight() then
		p_y = love.graphics.getHeight() - player:getHeight()
	end
end

function love.draw()
	love.graphics.draw(player, p_x, p_y)
end

love.timer.sleep(5)

function love.load()
	stillhere = love.graphics.newImage("waysh.png")
	stillhere_x = 100
	stillhere_y = 100
end
So what did I do wrong, and how do I read those messages so i don't need help anymore?

Re: Reading Error Messages

Posted: Fri May 27, 2016 10:47 pm
by Nixola
I can't help you with the code itself right now, but an error message basically tells you the file (main.lua) and the line (43) in which the error occurs. The traceback shows you the "stack", the sequence of functions before the error.
As I said, in this case the error is in the 43rd line in your main.lua.

Re: Reading Error Messages

Posted: Fri May 27, 2016 10:50 pm
by NatEye
Nixola wrote:I can't help you with the code itself right now, but an error message basically tells you the file (main.lua) and the line (43) in which the error occurs. The traceback shows you the "stack", the sequence of functions before the error.
As I said, in this case the error is in the 43rd line in your main.lua.
But I don't know why line 43 was wrong, I removed something about text and then it was fine.

Re: Reading Error Messages

Posted: Fri May 27, 2016 11:15 pm
by davisdude
It means that one of the values on line 43 is nil. That value would be "p_x", as the other value on line 43 is clearly a number.

The problem is that you define love.load twice, so the second one overwrites the first; preventing p_x from being set initially.

Re: Reading Error Messages

Posted: Sat May 28, 2016 1:32 pm
by Beelz
Also, aside from that issue... Instead of calling 'newImage' every time you change directions, you could make a table of the sprites and have a variable for which to use:

Code: Select all

local img = {
	idle = love.graphics.newImage('playerIdle.png'),
	up = love.graphics.newImage('playerUp.png'),
	down = love.graphics.newImage('playerDown.png'),
	left = love.graphics.newImage('playerLeft.png'),
	right = love.graphics.newImage('playerRight.png')
}

player.img = 'idle'

-- In love.update
if love.keyboard.isDown('w') then player.img = 'up'
elseif love.keyboard.isDown('s') then player.img = 'down'
elseif love.keyboard.isDown('a') then player.img = 'left'
elseif love.keyboard.isDown('d') then player.img = 'right'
else player.img = 'idle'
end

-- Then in love.draw
love.graphics.draw(img[player.img], player.x, player.y)