How to create and use 'Global' Constants? (SOLVED)

What’s the best way to define game-wide constants/enums that are accessible to all scripts?

For example, scripts attached to a map game object, a player game object and a hud game object might all need to know the map’s tile size. Using Lua in the past I’ve put definitions such as TILE_SIZE = 32 or NORTH = {0, 1} in a ‘constants.lua’ file and 'require’d that file in each script as needed. Is that a sensible way to do things in Defold?

( I realize there aren’t really such things as constants in Lua but I my own convention of defining them in uppercase has never caused a problem :smiley: )

1 Like

I would suggest you use modules.

in your constants.lua

local M = {}
M.TILE_SIZE = 32
M.NORTH = {0, 1}
return M

Then whenever you need to access those constants, you require them

local constants = require("constants")

print(constants.TILE_SIZE)
7 Likes

Ok, just as I’ve always done then - great! :smiley:

2 Likes

Technically you could also do

constants = require("constants")

at only one location in your code. The global is accessible from any script.

1 Like

… provided the required file is in the root folder?

I had to specify the path ( C_ = require"/main/constants") as it seemed to make more sense to keep it alongside the other scripts in /main - might help someone else :smiley:

No. All script component code is evaluated when the components are loaded at startup. Try placing a print() at top level and you’ll see. The location of the script file is irrelevant. All scripts are evaluated in the same Lua context (you can change that with the “Shared state” setting).

But require takes a “path” as argument, if that is what you meant?

require "main.constants"

will load the file “main/constants.lua” starting from the root of the project. See https://www.lua.org/pil/8.1.html

1 Like

Yes thanks, sorry I didn’t make it clearer.

What’s the benefit of this compared to just declaring the constants as global variables?

Can think of three points.

  1. You don’t “polute” the global name space (some people think it is more important than others).
  2. You could accidentally overwrite/set the value, this could be incredibly hard to debug.
  3. Explicit is better than implicit

Some modules I think it makes sense to put into the global space, but I try to keep them to a minimum. Personally I like to have things explicit.

4 Likes
  1. With the Defold editor’s “Referencing Files…” feature, you can see exactly which scripts might be accessing or modifying the values.
  2. If you know how to use metatables, you have the option to make read-only values, or use setter and getter functions.
2 Likes