I can't load images.

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
User avatar
SoraTheDev
Prole
Posts: 2
Joined: Wed May 24, 2017 7:07 pm

I can't load images.

Post 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.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: I can't load images.

Post 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.)
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
SoraTheDev
Prole
Posts: 2
Joined: Wed May 24, 2017 7:07 pm

Re: I can't load images.

Post by SoraTheDev »

It worked! Thank you very much! :)
Post Reply

Who is online

Users browsing this forum: No registered users and 49 guests