So I come from Unity and I’m very used to work with singleton managers in my games and I was wondering if this was possible in Luau.
Let’s say I have this script below, I feel like every time I would require this script somewhere to use it it would reassign my _instance variable, and then it wouldn’t really work right? Or am I not getting the gist of how metatables work? Thanks
-- Singleton.lua
local Singleton = {}
Singleton.__index = Singleton
local _instance
function Singleton.new()
local self = setmetatable({}, Singleton)
-- Initialize your private data here
return self
end
function Singleton:getInstance()
if _instance == nil then
_instance = Singleton.new()
end
return _instance
end
return Singleton
I’m not really sure what you meant because, even though I’ve used Unity before, I somehow never heard of singletons before, although passing values through ModuleScript functions could work:
Main script inside your object or anywhere else:
local ServerScriptService = game:GetService("ServerScriptService")
local ModuleScriptName = require(ServerScriptService.ModuleScript) -- Or any other location in which you placed the module script
local Object = script.Parent -- Or any other object, doesn't have to be the script's parent
ModuleScriptName.MyFunction(Object) -- Send the object to the ModuleScript
ModuleScript:
local MyModule = {}
function MyModule.MyFunction(Object)
Object.Color = Color3.new(0, 0.568627, 1) -- Make Object Blue
end
return MyModule
Hopefully I understood what you meant correctly and hopefully this helps!
When you require a ModuleScript, the result is cached, so when you require it later on, the same table will be returned (but the client and server get their own copy). What you decide to do is stylistic, but I generally don’t use metatables or the OOP idiom in a singleton and instead stick to using purely the dot operator instead.
-- Private data would be up here
local Service = {}
function Service.GetInstance()
end
-- Initializing code here, like hooking to a signal
-- This would only be run on the first require call
return Service