why am I getting this error?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
jbskaggs
Prole
Posts: 17
Joined: Sat Dec 12, 2020 4:20 pm

why am I getting this error?

Post 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?
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: why am I getting this error?

Post 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
jbskaggs
Prole
Posts: 17
Joined: Sat Dec 12, 2020 4:20 pm

Re: why am I getting this error?

Post 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?
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: why am I getting this error?

Post 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.
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.
jbskaggs
Prole
Posts: 17
Joined: Sat Dec 12, 2020 4:20 pm

Re: why am I getting this error?

Post 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.
Post Reply

Who is online

Users browsing this forum: veethree and 60 guests