Page 1 of 1

I have no idea why I am getting this error, I am a new dev.

Posted: Sun Mar 28, 2021 10:55 pm
by moths
I have a table set up like this
sprites = {}
sprites.background = love.graphics.newImage('sprites/background.png')
sprites.player = love.graphics.newImage('sprites/player.png')
In draw I have this
function love.draw()
love.graphics.draw(sprites.background, 0, 0)

world:draw()
love.graphics.draw(sprites.player, px, py, .1, .1, sprites.player.getWidth() / 2, sprites.player.getHeight() / 2)
end
But I'm getting this error
Error

main.lua:56: bad argument #1 to 'getWidth' (Texture expected, got no value)


Traceback

[C]: in function 'getWidth'
main.lua:56: in function 'draw'
[C]: in function 'xpcall'
can somebody please tell me what's going on and how to fix it

Re: I have no idea why I am getting this error, I am a new dev.

Posted: Mon Mar 29, 2021 1:42 am
by togFox
what's line 56?

Re: I have no idea why I am getting this error, I am a new dev.

Posted: Mon Mar 29, 2021 2:49 am
by MrFariator
The error is because you used a dot (".") instead of a colon (":") when calling the getWidth and getHeight functions.

The corresponding line of code needs to be either of the two:

Code: Select all

-- with "."
love.graphics.draw(sprites.player, px, py, .1, .1, sprites.player.getWidth(sprites.player) / 2, sprites.player.getHeight(sprites.player) / 2)

-- with ":", the preferred method in a case like this, and is syntactically equivalent to the "." method
love.graphics.draw(sprites.player, px, py, .1, .1, sprites.player:getWidth() / 2, sprites.player:getHeight() / 2)
What the colon syntax does is implicitly send the invoked function the object it's attached to as the first function argument. As such, in this case it sends the löve object representing the texture. getWidth and getHeight both expect a texture as the first passed function argument, hence the error if you don't do either of the two above.