Creating Singletons

Quebec uses singletons as a place where your game logic lives. Singletons are essentially classes that only get instantiated once.

Your singletons can define lifecycle events, which get invoked by the Quebec main module. The best example for a lifecycle event is the start lifecycle, which gets invoked as soon as the singleton starts.

Defining a singleton

Singletons have to be parented to the folder you defined when you ran the Quebec main module. They must also be a ModuleScript and can't be nested. Here's example code of a singleton:

local Quebec = require(PathToQuebecModule)

-- Quebec.singleton(table) registers the singleton in the Registry
local Singleton = Quebec.singleton({
    lifecycles = { "start" }
})

-- We recommend using the dot pattern for functions and then adding a first
--   parameter called "self" instead of using a colon definition.
-- This normally improves autocompletions.
function Singleton.init(self: typeof(Singleton))
    -- optional initialization function
    -- note: this function is ran before any singleton runs. yielding here
    --       will halt the main Quebec thread
end

-- the lifecycle method being invoked on the start
function Singleton.start(self: typeof(Singleton))
    print("started")
end

return Singleton

Last updated