If a image is in another folder than the script, how will I load it?
Thanks
Load image from different directory
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Load image from different directory
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:
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.
Re: Load image from different directory
this should work:
It's a bit long, but it worked for me.
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 )
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Load image from different directory
This is neat and all, but what's self in that context?MoseGames wrote: ↑Thu Feb 08, 2018 5:11 am this should work:It's a bit long, but it worked for me.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 )
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 True 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.
Who is online
Users browsing this forum: Bing [Bot] and 4 guests