Reading Error Messages

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
NatEye
Prole
Posts: 2
Joined: Fri May 27, 2016 10:25 pm

Reading Error Messages

Post 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?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Reading Error Messages

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
NatEye
Prole
Posts: 2
Joined: Fri May 27, 2016 10:25 pm

Re: Reading Error Messages

Post 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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Reading Error Messages

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Reading Error Messages

Post 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)

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests