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:
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.
record: (context: Context, run: Run<Name, Data, Memory>) -> Recordcontext: Context— the machine's context.run: Run<Name, Data, Memory>— the new stay.run.datais 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.
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
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.16by default.entries: { HistoryEntry<Name, Record> }?— entries to start with, oldest first, such as thosegetEntriesreturned on the server, to go with a restored machine. Each is copied, and only the lastlimitare 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.
| Cause | Message | |
|---|---|---|
| Error | limit is less than 1 | Canopy.history: 'limit' must be 1 or more. |
| Error | entries is not a table | Canopy.history: 'entries' must be a list of entries. |
| Error | An entry has no number enteredAt | Canopy.history: each of 'entries' needs a number 'enteredAt'. |
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
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
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
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
history:endsWith(records: { Record }): booleanReturns 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
history:getDuration(count: number?, now: number?): numberReturns 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.
| Parameter | Type | Description |
|---|---|---|
count | number? | How many of the newest entries to measure. All of them by default, and all of them when count is larger than the history. |
now | number? | 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:
const isComboReady = history:endsWith(COMBO)
const isQuickEnough = history:getDuration(#COMBO) <= 2
if isComboReady and isQuickEnough then
return S.goTo("ultimate", skill)
endclear Added in 0.1.0
history:clear(): ()Removes every entry, for example when a combo should start over.
HistoryEntry Added in 0.1.0
type HistoryEntry<Name, Record> = {
name: Name,
record: Record,
enteredAt: number,
leftAt: number?,
}| Field | Type | Description |
|---|---|---|
name | Name | The recorded state. |
record | Record | What the state's record returned on entry. |
enteredAt | number | When the state was entered. |
leftAt | number? | 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.