access to config.lua

Hello,

I would like to know, if there is any way, how I can access config.lua file. More specificly, I would like to know how application.content.imageSuffix parameter is defined in current context.

I am trying to use it for implementing some sprite loader, which loads animations from sprite sheets. There will be available multiple sprite sheets, one for each resolution. I would like to work it just exactly like dynamic image resolution method display.newImageRect uses it - I am trying to find out best sprite sheet resolution automatically.

I have got working sprite loader already, but I have to tell it explicitly, how imageSuffix is defined. I want to avoid this redundancy.

More general, I would like to read also other parameters from config.lua, like fps. Is it possible?

Thank you. [import]uid: 104645 topic_id: 18451 reply_id: 318451[/import]

For basic questions it’s good to search the forums and blogs.

You have 100% control over the config.lua file as the developer for your app.

This should help.

http://blog.anscamobile.com/2011/01/dynamic-image-resolution-made-easy/

-David
[import]uid: 96411 topic_id: 18451 reply_id: 70799[/import]

Hello david97,

thank you for your reply. I already studied this article, but this is not exactly what I was asking.

I am trying to implement ready-to-use “class”, which will do all this hard work automatically and independetly on current settings in config.lua.

I will try to be more specific, see my abstract example:

-- config.lua will contain something like this:  
application = {  
 content = {  
 imageSuffix =  
 {  
 [""] = 1, -- basic resolution without suffix  
 ["@2"] = 2, -- doubled resolution with suffix  
 },  
 }  
}  
-- Lets have some implemetation of my SpriteLoader.  
-- And lets have 2 sprite sheets in diferent resolutions with their configs  
-- sprite\_sheet.png + sprite\_sheet.lua  
-- sprite\_sheet@2.png + sprite\_sheet@2.lua  
  
newSpriteLoader()  
  
 self = {}  
  
 self:loadScaledSprite(spriteSheetImg, spriteSheetConfig)  
 -- class automatically looks into config.lua and recognises,  
 -- which of sprite\_sheet.png and sprite\_sheet@2.png should load  
 end  
  
 return self  
end  
  
-- Lets use our loader as simple, as possible  
local spriteLoader = newSpriteLoader();  
local scaledSprite = spriteLoader:loadScaledSprite("sprite\_sheet.png", "sprite\_sheet.lua")  
-- When creating sprite sheet from data, it must be manually scaled up/down  
-- to save proportions. This should be automatically done without interaction  
-- with developer.  
-- Using other words, developer will only change the config.lua file and the rest of code  
-- should work as it is.  

I hope my question is more clear now. [import]uid: 104645 topic_id: 18451 reply_id: 70808[/import]

All the elements of the config.lua file are set by the developer and fixed. There are no API’s I’m aware of that allow a person to read this file.

An alternative is to perform the the scaling yourself based on an API call to get the platform description and screen size. I’ve done this in the past with great results. The FPS is a hard coded value in the config file with a default of 30 FPS. Most devices are challenged to work above 30 FPS, although higher frames per second are possible. Since you hard code these values, you can count on them being consistent to the practical limitations of a given device, meaning the device will try to keep up with the specified FPS.

I had one app where I checked the device model and screen size and programatically scaled the graphics and loading of the graphics resources by resolution as needed.

There is an API call to determine the current scaling factor, if that helps.

scalex = display.contentScaleX
scaley = display.contentScaleY
– display scale values
print(“scalex”, “scaley”, scalex, scaley)
Here’s another thread with code to detect the actual FPS of a device:
http://developer.anscamobile.com/forum/2011/02/28/fspmemory-profiler-swfprofiler-ported-corona

Not sure this helps, but you can either rely on Corona to manage the scaling and selection of assists for you or you can do this yourself in the app to have specific control of the scaling and resource asset selection.

-David [import]uid: 96411 topic_id: 18451 reply_id: 70814[/import]

I am affraid, I still need to know, which image resolutions are currently available. I see only one way - define it explicitely and redundantly in config.lua and in my code.

Thanks for your help anyways. [import]uid: 104645 topic_id: 18451 reply_id: 70816[/import]

The available image resolutions are the ones you provide. All image resolutions you provide are available to you in your code.

Corona does not change the resolution of your images, but can scale them up or down, if you choose to have Corona do this.

-David
[import]uid: 96411 topic_id: 18451 reply_id: 70822[/import]

This is simple. Change the last line of config.lua to be:

[blockcode]return application[/blockcode]

Then, to access these properties from your code:

[blockcode]
local appConfig = require( “config” )
local imageSuffix = appConfig.content.imageSuffix
print( imageSuffix )
[/blockcode]

If you need to access properties which may not exist, it’s safer to use a function to step through each nested property:

[blockcode]
function selectByNestedIndex( targetTable, … )
if ( targetTable == nil ) then return nil end

local objRef = targetTable

for i, v in ipairs( arg ) do
objRef = objRef [v]
if ( objRef == nill ) then return nill end
end

return objRef
end

function checkConfig()
local appConfig = require( “config” )
local imageSuffix = selectByNestedIndex (
appConfig, “content”, “imageSuffix”)
print( imageSuffix )
end
[/blockcode] [import]uid: 71767 topic_id: 18451 reply_id: 72045[/import]

I just tried it and it is working! :o) Thanks for the useful post, clayton.
EDIT : Unfortunately, I was celebrating too early. This works only in simulator. In compiled file on a device, the file config.lua does not exist at all, so you cannot include it with the ‘require’ statement…

As far as I know, coronona loads data from config.lua and uses it internally, but the file itself is not a part of the application. So only way, how to access variables in the config.lua, is via corona api, like e.g. display.contentWidth.

So after some investigation, this is the real feature request to Ansca: Could it be possible to pass variables from config.lua to the programmer via API in the future? [import]uid: 104645 topic_id: 18451 reply_id: 72116[/import]

This works only in simulator.

I’m really glad that you discovered this. I was well down the path of implementing something without having tested it on an actual device.

Really, Ansca just needs to modify Corona so that you can “require” config.lua just like any other lua file. Programmatic access to the config settings via API would probably add too many functions to the API. [import]uid: 71767 topic_id: 18451 reply_id: 72167[/import]

Anyways, I overcame this limitation by symlinking “config.lua” to another file such as “config-local.lua”, and then "require"ing “config-local.lua”. [import]uid: 71767 topic_id: 18451 reply_id: 72635[/import]

Thanks dejay.clayton for the symlink idea. I had not previously heard of this concept and it solved my problem. For anyone wanting to look further into this as a potential solution, here’s a couple of quick resources:

What is a symlink?

http://en.wikipedia.org/wiki/Symbolic_link

Super easy way of creating a symlink, through the finder on OSX, without even touching the terminal:

http://www.macworld.com/article/58177/2007/06/symboliclinks.html [import]uid: 69553 topic_id: 18451 reply_id: 82905[/import]