Page 1 of 1

"Attempt to compare number with nil" error with collision

Posted: Wed Aug 07, 2019 5:22 pm
by bandi72006
Hello guys,

I'm quite new to programming in LÖVE 2D and Lua and I started coding my first game on it. Everything was running smoothly until I added collision to my game. It now gives me this error: Player.lua:13: attempt to compare number with nil.

I know I somehow made a stupid mistake but I'm just not sure. Here is the code:

Code: Select all

 Player = Class{}

function Player:init(y,dy)
  self.x = 640
  self.y = wHeight/2
  self.dy = dy
  self.image = love.graphics.newImage('SpaceShip.png')
  self.width = 40
  self.height = 10
end

function Player:collide(paddle)
  if self.x > paddle.x + 100 or paddle.x > self.x + 40 then
    return false
  end

  if self.y > paddle.y + 100 or paddle.y > self.y + 10 then
    return false
  end

  return true
end


function Player:update()
  self.y = self.y + self.dy
  if (player.y >= wHeight) then
    player.y = wHeight - 15
  end
  if (player.y <= 0) then
    player.y = 0
  end
end



function Player:render()
  love.graphics.draw(self.image,wWidth/2,self.y,0,2.5,2.5)
end
I have used the collision in some other code and has work perfectly. But I have been stuck for many hours.

Thanks!

Re: "Attempt to compare number with nil" error with collision

Posted: Wed Aug 07, 2019 5:39 pm
by zorg
Hi and welcome to the forums!

How are you calling Player:collide? Seems like the issue is there; maybe you're calling it with a dot (.) and neglected to pass itself as the first parameter, or you are calling it with a colon (:) and neglected to pass the paddle table into it.

Re: "Attempt to compare number with nil" error with collision

Posted: Thu Aug 08, 2019 4:47 am
by bandi72006
Well this is all the stuff I have in my main.lua:

Code: Select all

Class = require "Class"

require "Player"
require "Asteroid"

FORCE = 0.2

BACKGROUND = love.graphics.newImage("background.png")
local SCROLL_SPEED = 5
local SCROLL = 1940

function love.load()
  love.window.setTitle("Mid-G")
  love.graphics.setDefaultFilter("nearest", "nearest")
  wWidth = 1280
  wHeight = 720
  love.window.setMode(wWidth, wHeight)
  player = Player(wHeight/2,0.1)
  asteroid1 = Asteroid()
end

function love.update()

  player:update()
  asteroid1:update()

  if Player:collide(asteroid1) then
    love.event.quit()
  end

  asteroid1:reset()
  SCROLL = (SCROLL + SCROLL_SPEED)%640
  if love.keyboard.isDown("escape") then
    love.event.quit()
  end

  if love.keyboard.isDown("space") then
    if player.y >= wHeight/2 + 5 then
      player.dy = player.dy + FORCE
    else
      player.dy = player.dy - FORCE
    end
  else
    if player.y >= wHeight/2 then
      player.dy = player.dy - FORCE
    else
      player.dy = player.dy + FORCE
    end
  end



end

function love.draw()
  love.graphics.draw(BACKGROUND,-SCROLL,0)
  asteroid1:render()
  player:render()
end

function debug(log)
  love.graphics.print(log)
end

Re: "Attempt to compare number with nil" error with collision

Posted: Thu Aug 08, 2019 4:49 am
by bandi72006
Also, the reason why it's called paddle in Player.lua and asteroid in main.lua is because I used the collision code from another project, and when I changed the paddle to asteroid, I thought that was the problem, so I left it as it is.

I set the function to love.event.quit() just so I know that the collision works.

Re: "Attempt to compare number with nil" error with collision

Posted: Thu Aug 08, 2019 9:47 am
by pgimeno
The error is not "attempting to index a nil value". The error is "attempting to compare number with nil".

One of the objects doesn't have an x or an y field.

The problem is most likely in Asteroid.lua, but rather than guessing. it would be nice if you post the whole project instead of the snippets where you believe the problem is.

Re: "Attempt to compare number with nil" error with collision

Posted: Thu Aug 08, 2019 11:11 am
by bandi72006
Here is the asteroid.lua:

Code: Select all

Asteroid = Class{}

function Asteroid:init()
  self.x = 1280
  self.y = love.math.random(0,wHeight)
  self.dx = 0
  self.image = love.graphics.newImage("Asteroid.png")
  self.dx = love.math.random(5,100)
  self.width = self.image:getWidth()
  self.height = self.image:getHeight()
end

function Asteroid:reset()
  if self.x <= 0 then
    self.x = 1280
    self.y = love.math.random(0,wHeight)
    self.dx = love.math.random(10,20)
  end

end

function Asteroid:update()
  self.x = self.x - self.dx
end

function Asteroid:render()
  love.graphics.draw(self.image,self.x,self.y)
end

I don't have any more code and I haven't left anything else out.

Re: "Attempt to compare number with nil" error with collision

Posted: Thu Aug 08, 2019 10:16 pm
by pgimeno
Please post the whole project as a .love file.

If you don't want to share the graphics, replace them with blank images.

Re: "Attempt to compare number with nil" error with collision

Posted: Fri Aug 09, 2019 3:16 am
by bandi72006
Here it is:
Game.love
(15.34 KiB) Downloaded 285 times

Re: "Attempt to compare number with nil" error with collision

Posted: Fri Aug 09, 2019 10:26 am
by pgimeno
Okay, I see what's wrong now. Nothing beats having the whole code for debugging :)

It should have been obvious, but it seems we all missed it. In main.lua, you have

Code: Select all

  if Player:collide(asteroid1) then
But that's not the right way to call it. The object is called player, the class is called Player. You're passing the class to the function instead of the object. The class doesn't contain x or y, hence the error.

Simply replace it with this:

Code: Select all

  if player:collide(asteroid1) then

Re: "Attempt to compare number with nil" error with collision

Posted: Fri Aug 09, 2019 11:19 am
by bandi72006
Holy heck I didn't see that.. THANKS SOOO MUCH!!

I knew it was a stupid mistake but it cost me 2 days of doing nothing!!!