Defining Data

Thread Safety

Sometimes you want your singletons to store data which changes overtime. To avoid your data from being overwritten by accident from Quebec, you can use the data field within your singleton. The framework will NEVER touch the data field and therefore your data is safe there.

Example Service Using The Data Field
local Service = {
    lifecycles = { "start", "onStep" },
    data = {
        counter = 0
    }
}

function Service:start()
    print("Counter is:", self.data.counter)
end

function Service:onStep(_dt)
    self.data.counter += 1
    
    print(self.data.counter)
end

return Service

Last updated