Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Dynamic or "smart" config.lua file
Started by thomas6 Nov 14 2018 10:09 AM

- - - - -
8 replies to this topic
dynamic smart config level editor
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 980 posts
  • Corona SDK

Hi!

 

Question for the pro's / staff out there: is it possible to create a sort of "smart" config.lua file?

 

For complex reasons (to do with level editor on Mac and game on iOS running off the same codebase) it would save me quite a bit of time if the width and height in the config.lua file would use different values, depending on wether or not the app is running in the simulator (on my macBook) or on my iPhone.

 

Would there be a good way to do this? Can we run "if" statements inside config.lua, for example? And does config.lua have access to calls like system.getEnvironment() ?

 

 



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,591 posts
  • Corona SDK

It is possible, but I don't generally think smart config files are a good idea.

 

Why?  Because, by changing the resolution in the config file dynamically, you can't rely on knowing the content-resolution in your code.  Thus all sizes, spacing,etc need to be determined programatically.  This is a lot of work, error prone, and  may result in ugly results in some cases.

 

Instead, I prefer to choose a single configuration and then accommodate that setup for all resolutions.  While there is still some programmatic work here, it is significantly simpler and one can make fixed decisions on most layout and sizing.

 

For me, there are two exceptions:

 

1. Pixel Perfect Requirement - In some cases, you need to ensure sub-pixel rounding is minimized/avoided.  To do this you can use this config.lua file (or a varation): https://github.com/Lerg/smartpixel-config-lua

 

 

2.  Distinct Platforms - Sometimes, you know most of your users will be on mobile, but you also have a set of users on desktop.  In the latter case, it can be difficult to make everything look great if you've put most of your thought into mobile.  In this case, you may want to set up your app/game as follows:

  • bifurcated config.lua - Selects one config for mobile devices and a different one for desktop.
  • bifucated design - In critical sections of you app/game interface/layout you may execute entirely different code for very different layout designs.  Again, one for mobile and the other for desktop.

 

Again, 99% of the time I make a single fixed resolution work on all platforms.

 

PS - Today, the biggest PITA for me has been the iPhone X and other super tall resolutions.  Having to make a game look good and be accessible on both 640 x 960 and 1125 x 2436 is not fun.

 

 

PPS - This is the code for the pixel-perfect solution from Sergey.

-- Author: Lerg - spiralcodestudio.com
-- GitHub: https://github.com/Lerg/smartpixel-config-lua

-- SmartPixel config.lua
-- Uses pixel perfect content scaling when possible

local w, h = display.pixelWidth, display.pixelHeight

local modes = {1, 1.5, 2, 3, 4, 6, 8} -- Scaling factors to try
local contentW, contentH = 320, 480   -- Minimal size your content can fit in

-- Try each mode and find the best one
local _w, _h, _m = w, h, 1
for i = 1, #modes do
    local m = modes[i]
    local lw, lh = w / m, h / m
    if lw < contentW or lh < contentH then
        break
    else
        _w, _h, _m = lw, lh, m
    end
end
-- If scaling is not pixel perfect (between 1 and 2) - use letterbox
if _m < 2 then
    local scale = math.max(contentW / w, contentH / h)
    _w, _h = w * scale, h * scale
end

application = {
    content = {
        width = _w,
        height = _h,
        scale = 'letterbox',
        fps = 60,
        imageSuffix = {
            ['@2x'] = 1.1,
            ['@4x'] = 2.1,
        }
    }
}

-- Uncomment to use the common content scaling
--[[
application = {
    content = {
        width = 320,
        height = 480,
        scale = 'letterbox',
        fps = 60,
        imageSuffix = {
            ['@2x'] = 1.1,
            ['@4x'] = 2.1,
        }
    }
}
--]]


[TOPIC: post.html]
#3

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 980 posts
  • Corona SDK

Hi Roaminggamer, and thanks for the answer.

 

I quite fully agree with you, but this really is an exceptional use case that is limited to my development environment. My level editor runs at 3840 x 2160 pixels, while my game runs at much lower resolutions ofcourse. However, the two apps run from a single code base. That way, if I tweak the code in an enemy class, the change is propagated to both my level editor (which plays the game in real time) and my game.

 

In final deployment my config file will of course not be dynamic for the reasons you mentioned above.

 

That being said, so it is possible? That's good news!



[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,591 posts
  • Corona SDK

Yes, as I said it is possible.

 

I too do this somtimes, so I guess that is exception 3 - One config setting for editing, another for playing/distribution.

 

Note, in this case I also generally use the 'custom device' simulator view option.  Then when editing, I decide on a resolution that fits my screen nicely and is still usable.

 

Just be very careful to test often using your distro config.  It is very easy to mess up  if you don't have a strong grasp of Corona's scaling features.



[TOPIC: post.html]
#5

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 770 posts
  • Corona SDK

to my knowledge, accessing the corona API from the config file is not recommended, which means the display API shouldnt be used.

 

i know it can be used and for the most parts work, but i've chosen to follow the recommendations so i dont.

 

RG has made many more apps than me though so i'd listen to him anyway, but thought i'd mention it anyway.



[TOPIC: post.html]
#6

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 980 posts
  • Corona SDK

Hi Anaqim, and also thanks for the reply.

 

Also with you, fully agree: it is not recommended, and I would never do this in a distro. However, for my debugging and development is works and is the perfect solution.

 

@RoamingGamer: my game runs in parallel on my level editor macBook and my iPhone all of the time, so it is tested thoroughly as it will be used by my end users.



[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,196 posts
  • Enterprise

Back in the day, we had the concept of using an if statement to choose custom widths and heights based on device type. This was unwieldy as the number of devices grew. Someone came up with a computed config.lua that greatly simplified this, however as we learned more about the pros and cons of this, we highly advise against computed config.lua files except under certain circumstances.

 

This sounds more like a candidate where you could use an if statement testing against the platform, but you're going to run into even more complications because you're going to want to simulate your mobile app on macOS so Corona won't know if you are wanting to simulate your mobile app or your level design tool.

 

What I would consider doing is using symbolic links and have all the shared files in a "core" folder, then have two separate projects for the app and the level tool. For instance you could have:

 

yourProject/core

yourProject/levelBuilder

yourProject/game

 

Then create a symlink inside of levelBuilder and game to core effectively giving you:

 

yourProject/levelBuilder/core

yourProject/game/core

 
Now levelBuilder and game can have their own build.settings, config.lua and main.lua and they can load images, audio, class and scene files from the core folder and code unique to the levelBuilder and game can be in files in their specific projects.  Now you can run two instances of Corona one loading the levelBuilder and one loading the game.
 
Rob


[TOPIC: post.html]
#8

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 980 posts
  • Corona SDK

Hi Rob, RoamingGamer and all others who will advise my against doing what I'm about to do :D

 

I do appreciate the advice very much, and I agree with your line of thinking, but the conditional config.lua does really fit the exact needs I have perfectly, so no need to look further.

 

Regarding the final application: rest assured that I will use a standard config.lua file and build my interface and app responsively using other API calls, as I always do.

 

@Rob, I won't go into the details here, but the codebase in my levelEditor and Game is too similar to split up, so the smart config option really is the way to go to prevent me from needed to apply any changes twice.



[TOPIC: post.html]
#9

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 980 posts
  • Corona SDK

But once again for clarity: thank you very much for the helpful answers. Your support and presence on this forum make all the difference!




[topic_controls]
[/topic_controls]