I’m trying to load a bundled .csv and .png files in a game to be released on both Facebook Instant and mobile. It works on Facebook Instant and in the local IDE.
I have created a test app which returns the same error:
ERROR:SCRIPT: /main/main.script:16: attempt to index local 'file' (a nil value)
stack traceback:
/main/main.script:16: in function </main/main.script:1>
Code:
function init(self)
local sys_info = sys.get_sys_info()
local filename = "test.txt"
if sys_info.system_name == "Android" or sys_info.system_name == "iPhone OS" then
local application_path = sys.get_application_path()
local file_path = application_path .. "/" .. filename
print( " Application path: " .. application_path )
print( " Absolute file path: " .. file_path )
local file = io.open(file_path, "rb")
local data = file:read("*all")
file:close()
pprint( data )
elseif sys_info.system_name == "HTML5" then
http.request(filename, "GET",
function( _self, _, response )
if response.status ~= 200 and response.status ~= 304 then
-- Error
else
pprint( response.response )
end
end)
elseif sys_info.system_name == "Windows" or sys_info.system_name == "Darwin" then
local file_path = "bundle_resources/common/" .. filename
local file = io.open(file_path, "rb")
local data = file:read("*all")
file:close()
pprint( data )
end
end
Unzipping the .apk reveals the bundled file in the root folder:
That’s an option. It would be neat if I could avoid it, though, because then I could have one code base which exports to both Android/iOS and Facebook Instant.
Thanks @AGulev. I tried to use “file:///android_asset/” instead as the path, but it still can’t find the file. I think I’m swimming against the current here, so it’s looking like it’s time to branch this game.
I use bundle resources in the Facebook Instant version of the game, to load external .png files on the fly after the game has started, to save download time. The original plan was to use the same code base for the mobile version. If I swapped to custom resources instead, I would lose the benefit of loading these files on demand, because they would also be bundled with the game.
All of this is theoretical now anyway, as I’ve branched the game and started working on a mobile only version.