I’m working on a combat system and I’ve been trying to make use of some of the concepts in client prediction and server reconciliation, it’s not great, but it’s something. Anyways, I’m trying to make it so clients can detect a change and do stuff when the state of another client changes for example ClientB is blocking ClientA needs to do show the blocking effect as well as be able to check ClientB is in the block state.
Here’s the code I currently have which makes use of attributes curious to know how effective this is or if there is a better way I can write this?
-- Switch to blocking state when block button is pressed
if Input:pressed(inputButtons.blockButton) then
player.fsm:blocking(inputButtons.blockButton, os.time())
end
-- When a state changes we send it to the server
self.fsm.onstatechange = function(self, event, from, to, input, t)
if from == to then return end
local e = {}
e.t = eventType.state
e.state = to
e.input = input
e.time = t
remoteEvent:FireServer(e)
end
eventHandler[eventType.state] = function(player, event)
local playerObj = Server:GetPlayerByInstance(player)
playerObj.transitions[event.state]()
local event = {}
event.t = eventType.state
event.state = playerObj.fsm.current
event.player = player
playerObj:sendEventToClients(event)
end
-- Server receives the change and updates its copy of the players state (if it's valid)
eventHandler[eventType.state] = function(player, event)
local playerObj = Server:GetPlayerByInstance(player)
-- If the player can enter this state than update the server copy (more checks will need to be implemented)
if playerObj.fsm:can(event.state) then
playerObj.transitions[event.state]()
end
-- Send the new or same state to all clients
local event = {}
event.t = eventType.state
event.state = playerObj.fsm.current
event.player = player
playerObj:sendEventToClients(event)
end
-- Server updates the state attribute
self.fsm.onstatechange = function(event, from, to)
MessageSystem:sendMessage("stateChange", from, self.character)
end
MessageSystem:registerListener("stateChange", function(from, character)
if self.character ~= character then return end
self.character:SetAttribute("State", from)
end)