Production / development environment variables?

Is there a way to conditionally run code based on the environment (“production / development”)?

local env = --[[ environment value ]]--

if env == "development" then
    -- run debug code
else
    -- run production code
end

There is os.getenv() that will get an environment variable. os.getenv() will work on Windows, OSX and Linux, but I don’t see how you’re supposed to set environment variable on mobile or HTML5.

Another option is to check if you’re running a debug or release build. You can do that by checking sys.get_engine_info().is_debug.

A third option would be to manually or via a script set a custom value in game.project and read that via sys.get_config().

7 Likes

With all the web frameworks I have worked with, there is a .env file which is committed to version control. Then when you or someone else wants to work on the project, you would clone from the source repository, rename .env to env file and update the contents depending on your local setup. On the production server you would have another env file, which will hold the production config. The main env file is never committed to version control and the whole app depends on this for functinality.

Would having an env-dev.lua module that gets committed to version control and then be renamed to env.lua work?

I want to be able to define different configuration on different work environments. For example, the Defold Nakama client for multipler runs on different ports on my different laptops for local development. I can define the port in the env.lua file (which isn’t version controlled) and forget about it. Right now, when I work on the project on Laptop A, I need to update the client.host. Then when I move to the other laptop, I need to change it back and so forth.

Is it OK to use a lua module for this purpose?

EDIT: But then you would need to make sure the env.lua for production is used when you bundle your app hmmmm ok nevermind guys.