I actually found a better more official way of doing this without all the regex sillyness needed to parse the path. Of the potential performance implications of using debug.getinfo()
.
Get the name of the file a storyboard is from in Corona?
-
05-03-2022 - |
Question
I have a storyboard file and I want to reference the name of the file that the storyboard is implemented in so that I can load a configuration file of the same base name.
Here is an example that is in a file called scene001.lua
-- Called when the scene's view does not exist:
function scene:createScene( event )
local screenGroup = self.view
local json = require("json")
-- load the book definition file
local contents = textFromFile("scene001.json")
local page = json.decode(contents)
image = buildPage(page)
screenGroup:insert(image)
image.touch = onSceneTouch
end
As you can see I have to hard code the "scene001.json"
. I want to load a configuration file of the with the same name as the current storyboard, how can I do this dynamically?
Solution 2
OTHER TIPS
You can use the debug
library, specifically the function debug.getinfo
, to retrieve information about the current file name. With a simple pattern, you can then replace the .lua
extension with .json
to open your custom file.
print( debug.getinfo(1,"S").short_src ) --> /path/to/scene001.lua.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow