Loading files from disk

The following code…

1 function UILoadObjectImage (filename, nodename)
2 filename = “assets/images/scene-objects/”…filename…".png"
3 print(filename)
4 local file = io.open(filename, “rb”)
5 local bytes = file:read("*a")
6 local png = image.load(bytes)

works when I do a build in the editor but in HTML bundle or even a Mac bundle it fails. I get an error on line 5. “attempt to index local ‘file’ (a nil value)” I thought it might be a path issue but it doesn’t seem to be – any ideas?

io.open will return the error message as a second argument if it fails, so you can add this to be sure:

local file, err = io.open(filename, "rb")
if not file then
	print(err)
	return
end

Are the files actually there in a bundle? You have a “Bundle Resources” folder set in your game.project or you are adding them manually after bundling?

2 Likes

I’m not using the Bundle Resources in game project, didn’t know I had to. Now trying to figure out how to set it properly.

These files will not be automatically bundled with your application unless you specify the folder to be included.

The best and simplest option that is guaranteed to work on all platforms would be to use the Bundle Resources field of game. project and load using sys.load_resource().