Skip to content

History ​

A history keeps the last stays in the states you choose to record, each with a value you pick, the data the state was entered with, and when it was entered and left. Read it from any state's functions to react to what happened before, such as a sequence of skills that turns the next one into its ultimate form.

The examples on this page use this setup. Data is Skill, the skill each event carries, and Record is Skill too, what each recorded state adds:

luau
const Canopy = require("@Canopy")

type Skill = "slash" | "kick" | "burst"
type Context = { history: Canopy.History<Name, Skill> }
type Event = "skillUsed" | "skillEnded"
type Name = "free" | "usingSkill" | "ultimate"

const S = Canopy.setup<<Context, Event, Name, Skill, Skill>>()

Recording a state ​

Give a state a record function. Each time the state is entered, the machine adds an entry to its history with what record returns. Only states with record are added, so the steps inside a skill do not fill the history.

luau
record: (context: Context, run: Run<Name, Data, Memory>) -> Record
  • context: Context — the machine's context.
  • run: Run<Name, Data, Memory> — the new stay. run.data is the data the state was entered with, such as the skill used.

record runs on entry, after the state's memory factory and before onStart. Return the data, or a copy of the parts of the context you want to keep: the context itself keeps changing after the entry is made. run.data is optional, because a state can be entered without data, so check it before returning it as a Record.

luau
const usingSkill = S.state({
	record = function(context, run)
		const skill = run.data

		assert(skill, "usingSkill is entered with the skill used")

		return skill
	end,
	on = {
		skillEnded = "free",
	},
})

Giving a machine a history ​

history Added in 0.1.0 ​

luau
S.history(options: HistoryOptions?): History<Name, Record>

Makes an empty history. Pass it to S.new in options.history, and put it in the context as well when the states need to read it. The history belongs to you, not to the machine: keep it to remember a combo across a respawn, when the character gets a new machine.

  • options — a table with:
    • limit: number? — how many entries it keeps, dropping the oldest. 16 by default.
    • entries: { HistoryEntry<Name, Record> }? — entries to start with, oldest first, such as those getEntries returned on the server, to go with a restored machine. Each is copied, and only the last limit are kept.

A restored machine does not link its current state to the entry for it, so that entry's leftAt stays nil when the state is left.

CauseMessage
Errorlimit is less than 1Canopy.history: 'limit' must be 1 or more.
Errorentries is not a tableCanopy.history: 'entries' must be a list of entries.
ErrorAn entry has no number enteredAtCanopy.history: each of 'entries' needs a number 'enteredAt'.
luau
const history = S.history({
	limit = 8,
})

const machine = S.new(SkillStates.tree, {
	history = history,
}, {
	history = history,
})

A machine without a history records nothing, and its record functions are not called.

Reading a history ​

getEntries Added in 0.1.0 ​

luau
history:getEntries(count: number?): { HistoryEntry<Name, Record> }

Returns the last count entries, or all of them, oldest first. Each entry is a copy.

getRecords Added in 0.1.0 ​

luau
history:getRecords(count: number?): { Record }

Returns what record returned for the last count entries, or all of them, oldest first.

getLast Added in 0.1.0 ​

luau
history:getLast(): HistoryEntry<Name, Record>?

Returns a copy of the newest entry, or nil when the history is empty.

endsWith Added in 0.1.0 ​

luau
history:endsWith(records: { Record }): boolean

Returns whether the newest records equal records, in order, compared with ==. A history with fewer entries than records returns false.

getDuration Added in 0.1.0 ​

luau
history:getDuration(count: number?, now: number?): number

Returns how long the last count entries, or all of them, took: from when the oldest was entered to when the newest was left. While the machine is still in the newest, it counts up to now, or ends where the newest started when now is not given. An empty history returns 0. Gaps between the entries, spent in states that are not recorded, are part of the time.

ParameterTypeDescription
countnumber?How many of the newest entries to measure. All of them by default, and all of them when count is larger than the history.
nownumber?The current time, used when the machine has not left the newest entry yet.

Called from record or from a handler as the last skill starts, it gives the time from the first skill of a sequence to the last:

luau
const isComboReady = history:endsWith(COMBO)
const isQuickEnough = history:getDuration(#COMBO) <= 2

if isComboReady and isQuickEnough then
	return S.goTo("ultimate", skill)
end

clear Added in 0.1.0 ​

luau
history:clear(): ()

Removes every entry, for example when a combo should start over.

HistoryEntry Added in 0.1.0 ​

luau
type HistoryEntry<Name, Record> = {
	name: Name,
	record: Record,
	enteredAt: number,
	leftAt: number?,
}
FieldTypeDescription
nameNameThe recorded state.
recordRecordWhat the state's record returned on entry.
enteredAtnumberWhen the state was entered.
leftAtnumber?When the state was left, or nil while the machine is still in it.

Use enteredAt and leftAt to require that a sequence was done quickly enough. See the Combo example.