I have a lua file containing lots of variables which I intend to reuse a lot (most of them store colour tables). So I might (ideally) have a long list that reads something like:-
[lua]
local purple = {0.8, 0.1, 0.7}
local teal = {0.05, 0.6, 0.5}
[/lua]
etc.
I have come across a circumstance where I would like to access these in another module in the simplest way possible. I’m fully aware that the normal way of doing this would be to store them in a table module, do local var = require (“scripts.variables”), and refer to them as (say) var.teal, var.purple etc… However that looks a lot uglier, is harder to read and is slower to type. I’m using these variables to create levels, and the quicker / easier it is to edit, the easier my life will be when fine tuning.
I wondered whether there was a way of packing and unpacking local scope variables (in a similar way to using _G to access globals). I have two questions:-
-
I’ve found a way to package the locals into a table using debug.getlocal. I thought I was on my way to a solution for the “unpacking” part using the debug library (debug.setlocal). However, on closer inspection this only seems to be able to change local variables which have already been declared. Is that right? Is my quest fruitless?
-
In any event I see lots of warnings about using the debug library in finished code, without much explanation around the dangers. Is this really something to be concerned about? My proposed use for it seems pretty innocuous but I would be interested to know if anyone else “resorts” to using the debug library in this way.