Skip to content

Character ​

A character is free, using a move, or knocked back. A move runs through three timed phases, and being knocked back interrupts any of them. The superstates hold the handlers their children share, so each phase only says where it goes next.

It also shows:

  • tags, so movement code asks hasTag("canMove") rather than listing states;
  • a cooldown on usingMove, so moves cannot be chained faster than once a second;
  • a timeline on active, which spawns the hitbox a tenth of a second in;
  • the setup's events and tags lists, which catch a misspelt on key or tag when the tree is built.
luau
const Canopy = require("@Canopy")

export type Context = {
	humanoid: Humanoid,
	move: string?,
}
export type Event = "moveStarted" | "moveCancelled" | "knockedBack" | "hitWall" | "landed"
export type Name =
	"free"
	| "usingMove"
	| "windUp"
	| "active"
	| "recovery"
	| "knockedBack"
	| "flying"
	| "wallImpact"
	| "groundImpact"

const S = Canopy.setup<<Context, Event, Name, string, nil>>({
	events = {
		"moveStarted",
		"moveCancelled",
		"knockedBack",
		"hitWall",
		"landed",
	},
	tags = {
		"canMove",
	},
})

-- Damages what the move hits. Your game's own hitbox code goes here.
const function spawnHitbox(humanoid: Humanoid, move: string?) end

const tree = S.tree({
	initial = "free",
	states = {
		free = S.state({
			tags = { "canMove" },
			on = {
				knockedBack = "knockedBack",
				moveStarted = "usingMove",
			},
		}),
		usingMove = S.state({
			initial = "windUp",
			cooldown = 1,
			on = {
				knockedBack = "knockedBack",
				moveCancelled = "free",
			},
			onStart = function(context, run)
				context.move = run.data
			end,
			onEnd = function(context)
				context.move = nil
			end,
			states = {
				windUp = S.state({
					duration = 0.3,
					onTimeUp = "active",
				}),
				active = S.state({
					duration = 0.2,
					onTimeUp = "recovery",
					timeline = {
						[0.1] = function(context)
							spawnHitbox(context.humanoid, context.move)
						end,
					},
				}),
				recovery = S.state({
					tags = { "canMove" },
					duration = 0.4,
					onTimeUp = "free",
				}),
			},
		}),
		knockedBack = S.state({
			initial = "flying",
			on = {
				knockedBack = "knockedBack",
			},
			states = {
				flying = S.state({
					on = {
						hitWall = "wallImpact",
						landed = "groundImpact",
					},
				}),
				wallImpact = S.state({
					on = {
						landed = "groundImpact",
					},
				}),
				groundImpact = S.state({
					duration = 0.8,
					onTimeUp = "free",
				}),
			},
		}),
	},
})

return {
	S = S,
	tree = tree,
}

Start a move with its name as the event's data. usingMove keeps it in the context while any phase runs, and clears it when the move ends, however it ends.

luau
const ReplicatedStorage = game:GetService("ReplicatedStorage")
const RunService = game:GetService("RunService")

const CharacterStates = require(ReplicatedStorage.CharacterStates)

const S = CharacterStates.S
const characters = S.group()

RunService.Heartbeat:Connect(function()
	characters:process(os.clock())
end)

const function track(humanoid: Humanoid)
	const machine = S.new(CharacterStates.tree, {
		humanoid = humanoid,
		move = nil,
	}, {
		at = os.clock(),
		hooks = {
			onEnter = function(context, name)
				context.humanoid:SetAttribute("State", name)
			end,
		},
	})

	characters:add(machine)

	humanoid.Died:Once(function()
		-- A stopped machine leaves its group by itself.
		machine:stop()
	end)

	return machine
end

return track

machine:send("moveStarted", "uppercut") enters usingMove, then windUp. machine:send("knockedBack") during any phase leaves the phase and usingMove, and enters knockedBack and flying. The group processes every character in one call, and skips the ones only waiting for an event.

A player who presses a move a moment too early, during recovery or the cooldown, expects it to come out once it can. Send the input with a buffer, and read the tag to set the walk speed:

luau
machine:send("moveStarted", "uppercut", os.clock(), {
	buffer = 0.2,
})

humanoid.WalkSpeed = if machine:hasTag("canMove") then 16 else 0