"Attempt to compare number with nil" error with collision

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
bandi72006
Prole
Posts: 6
Joined: Wed Aug 07, 2019 5:13 pm

"Attempt to compare number with nil" error with collision

Post by bandi72006 » Wed Aug 07, 2019 5:22 pm

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!
Attachments
Screen Shot 2019-08-07 at 9.19.50 PM.png
Screen Shot 2019-08-07 at 9.19.50 PM.png (59.7 KiB) Viewed 2336 times

User avatar
zorg
Party member
Posts: 2755
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post by zorg » Wed Aug 07, 2019 5:39 pm

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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

bandi72006
Prole
Posts: 6
Joined: Wed Aug 07, 2019 5:13 pm

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

Post by bandi72006 » Thu Aug 08, 2019 4:47 am

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

bandi72006
Prole
Posts: 6
Joined: Wed Aug 07, 2019 5:13 pm

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

Post by bandi72006 » Thu Aug 08, 2019 4:49 am

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.

User avatar
pgimeno
Party member
Posts: 1946
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

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

Post by pgimeno » Thu Aug 08, 2019 9:47 am

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.

bandi72006
Prole
Posts: 6
Joined: Wed Aug 07, 2019 5:13 pm

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

Post by bandi72006 » Thu Aug 08, 2019 11:11 am

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.

User avatar
pgimeno
Party member
Posts: 1946
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

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

Post by pgimeno » Thu Aug 08, 2019 10:16 pm

Please post the whole project as a .love file.

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

bandi72006
Prole
Posts: 6
Joined: Wed Aug 07, 2019 5:13 pm

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

Post by bandi72006 » Fri Aug 09, 2019 3:16 am

Here it is:
Game.love
(15.34 KiB) Downloaded 31 times

User avatar
pgimeno
Party member
Posts: 1946
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

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

Post by pgimeno » Fri Aug 09, 2019 10:26 am

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

bandi72006
Prole
Posts: 6
Joined: Wed Aug 07, 2019 5:13 pm

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

Post by bandi72006 » Fri Aug 09, 2019 11:19 am

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!!!

Post Reply

Who is online

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