Usage
Server data updated immutably
local gameStats = {
round = 0,
playerCount = 0,
}
local lastSent = gameStats
local function incrementRound()
gameStats = Sift.Dictionary.merge(gameStats, {
round = gameStats.round + 1,
})
end
local function onPlayerAdded(player: Player)
-- When a new player joins, we can send them a diff between nil and gameStats.
local initialDiff = DeltaCompress.diffImmutable(nil, gameStats)
ReplicatedStorage.RoundInfoUpdateRemote:FireClient(initialDiff)
end
RunService.Heartbeat:Connect(function()
if lastSent == gameStats then
-- gameStats hasn't changed.
return
end
-- Since data is update immutably, there should always be a diff.
local diff = DeltaCompress.diffImmutable(lastSent, gameStats)
lastSent = gameStats
ReplicatedStorage.RoundInfoUpdateRemote:FireAllClients(diff)
end)
Server data updated mutably
local gameStats = {
round = 0,
playerCount = 0,
}
local lastSent = nil
local function incrementRound()
gameStats.round += 1
end
local function onPlayerAdded(player: Player)
-- When a new player joins, we can send them a diff between nil and gameStats.
local initialDiff, updatedLastSent = DeltaCompress.diffMutable(nil, gameStats)
lastSent = updatedLastSent
ReplicatedStorage.RoundInfoUpdateRemote:FireClient(initialDiff)
end
RunService.Heartbeat:Connect(function()
local diff, updatedLastSent = DeltaCompress.diff(lastSent, gameStats)
if diff == nil then
-- gameStats hasn't changed.
return
end
lastSent = updatedLastSent
ReplicatedStorage.RoundInfoUpdateRemote:FireAllClients(diff)
end)
Client
local gameStats = nil
ReplicatedStorage.RoundInfoUpdateRemote.OnClientEvent(function(diff)
-- The diff can be applied immutably or mutably depending on your usecase!
gameStats = DeltaCompress.applyImmutable(gameStats, diff)
end)