I have two seperate module scripts, one that deals with the time, one that deals with the status. Problem I’ve just realised is both use the same set of variables (the ones getting the stuff inside the GUI)
But if I was to move around parts in the GUI, change names, etc. I’d have to go back into both and fiddle around the variables and what not to get them right, and worse if I have more scripts that all reference the same GUI. Is there a way to store like the variables inside a script or module or table or something, and have that passed around to all the modules? so if I change anything in the gui I can just change it in one script and all the other scripts will just look off the 'variables script?
local Variables = require(commonvars)
print(Variables.var)
or if for some reason youre super lazy and like global variables
local _ENV = getfenv()
for k,v in next,require(commonvars) do
_ENV[k] = v --NOT RECOMMENDED, this is what Luas old 'module()' could do
--Problems with conflicting global names and also less readable and typically
--shown as an undefined variable with the syntax highlighter
--Also harder to debug name changes and other issues
end
print(var)
I’d design it so that I can pass in the signal to listen to and the text label to output to. TimerManager would then look like:
local TimerManager = {}
function TimerManager:connectTimeChanged(signal, textLabel)
signal:Connect(function(newValue)
-- Code here
textLabel.Text = ...
end)
end
return TimerManager
And then in your main script, do something like:
-- Get replicated storage etc.
local updateTimer = events:WaitForChild("UpdateTimer")
-- Get the gui objects etc.
local timer = timerBar:WaitForChild("Timer")
-- Set up the timer manager
local TimerManager = require(Modules.TimerManager)
TimerManager:connectTimeChanged(UpdateTimer.OnClientEvent, timer)
The benefit you get from doing it this way, is it’s now testable:
-- Simple implementation of a custom signal
local Signal = {}
Signal.__index = Signal
function Signal.new()
return setmetatable({
connections = {}
}, Signal)
end
function Signal:connect(func)
self.connections[func] = true
end
function Signal:fire(...)
for func, _ in ipairs(self.connections) do
func(...)
end
end
local function testTimeChanged()
local TimerManager = require(Modules.TimerManager)
local testLabel = Instance.new("TextLabel")
local signal = Signal.new()
TimerManager:connectTimeChanged(signal, testLabel)
signal:fire(fakeData)
assert(testLabel.Text == "some example")
testLabel:Destroy()
end