Recipes
Short solutions to jobs that come up with most machines. Each one stands on its own, so copy the one you need. They use the tree from the Character example.
Test with exact times
A machine reads no clock, so a test passes the times itself. Start the machine at 0 and process it at any moment.
const ReplicatedStorage = game:GetService("ReplicatedStorage")
const CharacterStates = require(ReplicatedStorage.CharacterStates)
const S = CharacterStates.S
const machine = S.new(CharacterStates.tree, {
humanoid = Instance.new("Humanoid"),
move = nil,
}, {
at = 0,
})
machine:send("knockedBack", nil, 0)
machine:send("landed", nil, 1)
machine:process(1.5)
assert(machine:isIn("groundImpact"), "still on the ground")
machine:process(1.8)
assert(machine:isIn("free"), "up again at 1.8")React to a group of states
Ask about the superstate instead of listing its children. isIn is true for a state and every state inside it.
return function(machine): boolean
const isBusy = machine:isIn("usingMove")
const isKnockedBack = machine:isIn("knockedBack")
return not isBusy and not isKnockedBack
endAsk what a state allows with tags
Rather than listing the states where something is allowed, give those states a tag. A state's tag holds for every state inside it, and adding a new state needs no change to the code that asks.
return function(machine): boolean
return machine:hasTag("canJump")
endPass data into a state
The data of send or goTo reaches every state the move enters, as run.data. A memory factory gets it too, so a state can keep what it was entered with.
const usingMove = S.withMemory(function(context, moveName)
return {
moveName = moveName,
}
end):state({
initial = "windUp",
states = {
-- ...
},
})Replicate the state
Use the onChanged hook to tell clients about each move, once per move instead of once per state entered.
const machine = S.new(CharacterStates.tree, context, {
at = os.clock(),
hooks = {
onChanged = function(context, path)
context.humanoid:SetAttribute("StatePath", table.concat(path, "/"))
end,
},
})Send the state to a player who joins late
onChanged only tells clients about the moves after they joined. Send a joining player a snapshot instead, and make a machine there with restore: it is in the same states, with the same time left on each duration and timeline.
Players.PlayerAdded:Connect(function(player)
StateRemote:FireClient(player, machine:getSnapshot())
end)StateRemote.OnClientEvent:Connect(function(snapshot)
machine = S.restore(CharacterStates.tree, context, snapshot)
end)Pause with the game
Pause a machine to freeze its durations and timelines, such as during a cutscene, and slow it for slow motion. Pausing and slowing moves each end time, so nothing ends early or late.
machine:pause(os.clock())
playCutscene()
machine:resume(os.clock())
machine:setTimeScale(0.25) -- Slow motionFind out why a machine moved
Keep a trace of the last events, and print it when something looks wrong. Each entry says which state took the event, and whether the machine moved.
const machine = S.new(tree, context, {
trace = 10,
})
for _, entry in machine:getTrace() do
print(entry.event, entry.handledBy, entry.hasMoved)
endRun two concerns at once
A character that can move and attack at the same time needs a state for every pair in one tree. Split them into parallel regions instead, one tree each, sharing one context.
Stop a machine with its owner
A stopped machine leaves every state, running each onEnd, so cleanup in onEnd runs when the owner goes away too.
model.Destroying:Once(function()
machine:stop()
end)