Scoping issues in Lua while tidying code
Posted: Wed Nov 02, 2016 4:55 pm
Hi folks,
I'm in the midst of a new Löve project and trying to use fewer globals this time around. :-)
As such, I've made my modules something along the lines of this:
In my main.lua:
The result I get is:
This is presumably because 'require' gave me an 'old' version of module.a, and the newer version has not been assigned? As such, this is clearly the wrong way to get module variables.
I could always grab them as a table via a function, I suppose, but is there a more elegant way to expose variables like this?
I'm in the midst of a new Löve project and trying to use fewer globals this time around. :-)
As such, I've made my modules something along the lines of this:
Code: Select all
local module = {}
local a = 0
local function doThing()
print("Hello")
a = 5
end
module.a = a
module.doThing = doThing
return module
Code: Select all
local mod = require("module")
function love.load()
mod.doThing()
print(mod.a)
end
Code: Select all
"Hello"
0
I could always grab them as a table via a function, I suppose, but is there a more elegant way to expose variables like this?