Page 1 of 1

why am I getting this error?

Posted: Tue Dec 15, 2020 5:19 pm
by jbskaggs
I know I am missing something completely fundamental and obvious, but Im getting why this doesnt work and I have gone thru quite a few examples and videos and I am only getting more confused.

I am getting this error

Code: Select all

Error

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


Traceback

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

in this little script:

Code: Select all

--create our tables, image here is just a name

myImage={}
mouse={}



--loads our myImages
function love.load()
  
myImage.image = love.graphics.newImage("hand.png")
myImage.background= love.graphics.newImage("800x600.jpg")



-- here I define hero as a image
hero= love.graphics.newImage("hero.png")
movex=0
movey=0
stepx=0
stepy=0

end

function love.mousereleased(x, y, button)
   if button == 1 then
   movex=x
   movey=y
   end
end

--get our mouse position
function love.update()
love.mouse.setVisible( false )
mouse.x, mouse.y = love.mouse.getPosition()


-- here is where the bug occurs
    if hero.x < mouse.x then stepx=stepx+4 
      
   
      end
 
end


function love.draw()
  --draw background
  love.graphics.draw(myImage.background,0,0)
  --draw image to mouse position
  love.graphics.draw(myImage.image,mouse.x-40,mouse.y-10)
  --draw hero and move towards
  love.graphics.draw(hero,stepx,stepy)
  
  
  end

I am assuming somehow hero.x is nil- but I dont know why its nil. Wanting to check hero.x position in relation mouse.x position.

So how to create the hero reference in such a way I can see and check coordinates etc?

Re: why am I getting this error?

Posted: Tue Dec 15, 2020 6:02 pm
by sphyrth
You don't have "hero.x" defined.

Either you do something like this...

Code: Select all

hero = 
{
  image = love.graphics.newImage("hero.png"),
  x = 0,
  y = 0
}
or convert the bugged section into something like this:

Code: Select all

if stepx < mouse.x then stepx=stepx+4 
end

Re: why am I getting this error?

Posted: Tue Dec 15, 2020 6:09 pm
by jbskaggs
Awesome and thanks!

I think is what I am look for.

Code: Select all

hero = 
{
  image = love.graphics.newImage("hero.png"),
  x = 0,
  y = 0
}
I shall try it out.

So you create a table and add variables to it. X and y are not set, but is x and y reserved variables that always refer to 2d coordinates?

Re: why am I getting this error?

Posted: Tue Dec 15, 2020 7:11 pm
by zorg
jbskaggs wrote: Tue Dec 15, 2020 6:09 pm So you create a table and add variables to it. X and y are not set, but is x and y reserved variables that always refer to 2d coordinates?
No, they are not reserved, nor is anything else; you creating an Image object doesn't automatically allow you to access its position (or any other parameter) just by doing something like hero.x (and images don't even store positions).

Feel free to look at the wiki, it tells you what objects support what methods that can get you some pieces of data - like image:getWidth(), image:getHeight(), image:getDimensions() - since it's super useful.

Re: why am I getting this error?

Posted: Tue Dec 15, 2020 7:34 pm
by jbskaggs
Thanks that is helpful. Like I said I was getting lost in the wiki and videos. But these replies are very illuminating for me as in GML x and y were reserved and it was biasing my thinking.