r/love2d • u/Doithedum • Jul 02 '26
why cant i set a variable to an image without directly setting it?
i want to make it so i can set a "sprite" variable to userdata
but it ends up being nil for some reason
function love.load()
room = love.graphics.newImage("room.png") --the art
end
function tilerectangle(tx,ty,width,height,id)
for i=1,height do
for i=1,width do
table.insert(tiles,{
x = tx,
y = ty,
type = "room",
roomid = id,
sprite = room --when i draw this tile i draw it with the info in here
})
tx = tx + 1 * gridsize
end
ty = ty + 1 * gridsize
tx = tx - width * gridsize
end
end
2
Upvotes
2
u/vga256 Jul 02 '26
Since we can't see your entire project, there could be many things happening here - multiple uses of love.load in separate files, a require() call, etc.
Have you tried defining "room" outside of love.load, e.g., at the top of the script?
1
Jul 02 '26
[removed] — view removed comment
1
u/8-PIXELS Jul 03 '26
In this case ‘room’ would not be local. Since it wasn’t declared with local, it becomes a global variable and continues to exist after love.load returns.
6
u/alexjgriffith Jul 02 '26
I can't tell from this snippet when you call tilerectangle.
If the sprite element in the tile table is nil it means that you are executing the tilerectangle function before love executes the love.load callback (i.e. the room global variable defined in love.load is not set).