Load image from different directory

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
PartyCow
Prole
Posts: 18
Joined: Sat Nov 17, 2012 3:12 pm

Load image from different directory

Post by PartyCow »

If a image is in another folder than the script, how will I load it?

Thanks
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Load image from different directory

Post by Robin »

If you're saying what I think you're saying then you can't.

Otherwise: suppose your game is at "(....)/mygame/". Then your main.lua would need to be at "(....)/mygame/main.lua". If you have an image at "(....)/mygame/images/background.png", you would load it as:

Code: Select all

background = love.graphics.newImage("images/background.png")
Help us help you: attach a .love.
MoseGames
Prole
Posts: 3
Joined: Tue Feb 21, 2017 2:43 am

Re: Load image from different directory

Post by MoseGames »

this should work:

Code: Select all


local file = io.open(self:path(path) , "r")
local contents = file:read("*all")
local data = love.filesystem.newFileData( contents, "img.png", "file" )
local imgdata = love.image.newImageData( data )

file:close()

local img = love.graphics.rawNewImage( imgdata )
It's a bit long, but it worked for me.
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Load image from different directory

Post by zorg »

MoseGames wrote: Thu Feb 08, 2018 5:11 am this should work:

Code: Select all

local file = io.open(self:path(path) , "r")
local contents = file:read("*all")
local data = love.filesystem.newFileData( contents, "img.png", "file" )
local imgdata = love.image.newImageData( data )
file:close()
local img = love.graphics.rawNewImage( imgdata )
It's a bit long, but it worked for me.
This is neat and all, but what's self in that context?
Also, what's love.graphics.rawNewImage??? last i checked, only love.graphics.newImage exists.

Anyway, io.open(path, 'r') is fine, since it does take a path, no need to make it complicated.

That said, you should only use lua's io functions if you want to access files from directories ABOVE/OUTSIDE the one where your main.lua is; if it's a sub-directory, then you can access those simply by doing love.image.newImage('subfolder/image.png').
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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot] and 5 guests