Page 1 of 1

I can't load images.

Posted: Wed May 24, 2017 7:23 pm
by SoraTheDev
Hello! I am currently having a problem with my first script. I am trying to load an image, but it returns this error:

Code: Select all

main.lua:10 bad argument #1 to 'draw' (Drawable expected, got nil)
This is my code:

Code: Select all

function love.load()
	local me = love.graphics.newImage("me.png")
end
function love.draw()
	local defaultx = 348
	local defaulty = 300
	local x = 348
	local y = 600
	love.graphics.print("Hello World!", x, y)
	love.graphics.draw(me, defaultx, defaulty)
end
How can I solve this? Thank you.

Re: I can't load images.

Posted: Wed May 24, 2017 9:08 pm
by Nixola
The "me" variable is local to love.load; that means it won't exist outside of that function. While using locals is good, and too many global variables can make for a tricky environment to develop in, having a variable with a wider scope isn't a bad thing. (In this case, easy solution: declare the variables like this:

Code: Select all

local me, defaultx, defaulty, x, y
at the top of your main.lua file, then use them as normal in the rest of the code without declaring them again; without the "local" keyword. That means the variables won't be global, but still be available to the whole file.)

Re: I can't load images.

Posted: Thu May 25, 2017 5:05 pm
by SoraTheDev
It worked! Thank you very much! :)