Recently I came to the problem with editing configurations of a ModuleScript.
For example, I want to toggle if headtilt is on in the ModuleScript HeadTilt.
The original method I used is to have a centre controller module that stores all the configurations of different client modules.
local Controller = {}
local self = Controller
Controller.Configs = {
HeadTiltEnabled = true,
}
Controller.EditConfig = function(Config,Value)
assert(self.Configs[Config],"Config doesn't exist")
assert(typeof(Value)==typeof(self.Configs[Config]),"Type doesn't match")
self.Configs[Config] = Value
--re-initializes the module that has config being edited--
--in my code I uses separate tables to store configs so--
--knowing which module to init is easy--
end
return Controller
For ease of reading I simplified the table structure, instead of using a single table I actually uses one for each module.
With this, in my modulescripts, I can simply do
local HeadTilt = {}
local self = HeadTilt
local Controller = require(game.ReplicatedStorage.Example.Controller)
HeadTilt.Init = function ()
local HeadTiltEnabled = Controller.Configs.HeadTiltEnabled
--unbinds function from renderstep--
if HeadTiltEnabled then
--binds headtilt function to renderstep--
end
end
return HeadTilt
This method works totally fine but recently I saw someone’s modulescript using bindable events to do configurations, for example he parented a EditConfig BindableEvent under the modulescript, and in the modulescript there is
local EditConfig = script:WaitForChild("EditConfig")
EditConfig.Event:Connect(function(Config,Value)
--asserting--
self.Configs[Config] = value
end
I wonder which one would be better, in case of performace and organisation etc.?
What I think is using BindableEvents will save you debugging from requiring the controller module that may cause some glitches, but really is it? What are some advantages and disadvantages of using each method?
Any help on the topic will be appreciated!