Persistent<T extends PersistentContent>
In the past, we could only modify the state of Storable through Script, but Persistent provides a set of useful methods to easily control the flow.
Persistent requires the name and initial state of a namespace. Persistent with the same name will share the same state.
⚠️
The namespace cannot be an existing built-in namespace (built-in namespaces include: "game")
type PersisData = {
flag: boolean;
coin: number;
name: string;
// ...
};
const persis = new Persistent<PersisData>("persis", {
flag: false,
coin: 0,
name: "John Smith",
// ...
});
Note: The stored content must be serializable, the serializable values supported by NarraLeaf-React include:
string
number
boolean
object
: key isstring
, value is the above serializable valuearray
: elements are the above serializable valueundefined
null
Date
: standard JavaScript date object
⚠️
If the script content contains Persistent, it must be registered with Story.
story.registerPersistent(persis);
Public Methods
constructor
namespace: string
- the name of the namespacedefaultContent: T
- initial state
equals<K extends StringKeyOf<T>>
key: K
- the key of the statevalue: T[K]
- the value of the state- Returns:
Lambda<boolean>
scene.action([
Condition
.If(persis.equals("name", "John Smith"), [
character1.say("Hello, John Smith!")
])
]);
notEquals<K extends StringKeyOf<T>>
key: K
- the key of the statevalue: T[K]
- the value of the state- Returns:
Lambda<boolean>
isTrue<K extends StringKeyOf<T>>
key: K
- the key of the state- Returns:
Lambda<boolean>
scene.action([
Condition
.If(persis.isTrue("flag"), [
character1.say("Flag is true")
])
]);
isFalse<K extends StringKeyOf<T>>
key: K
- the key of the state- Returns:
Lambda<boolean>
isNotNull<K extends StringKeyOf<T>>
key: K
- the key of the state- Returns:
Lambda<boolean>
toWords<K extends StringKeyOf<T>>
key: K
- the key of the state- Returns:
Word<DynamicWord>
character.say`You have ${persis.toWords("coin")} coins`;
get<K extends StringKeyOf<T>>
Alias of get
key: K
- the key of the state- Returns:
Word<DynamicWord>
character.say`Your have ${persis.get("coin")} coins`;
conditional
condition: Lambda<boolean> | LambdaHandler<boolean>
ifTrue: DynamicWordResult
ifFalse: DynamicWordResult
- Returns:
Word
character.say`Your flag is ${persis.conditional(
persis.isTrue("flag"),
"true",
"false"
)}`;
evaluate<K extends StringKeyOf<T>>
Evaluate the state with a JavaScript function
key: K
- the key of the statefn: (value: T[K]) => boolean
- a JavaScript function to evaluate the state- Returns
Lambda<boolean>
scene.action([
Condition
.If(persis.evaluate("coin", (coin) => coin < 10), [
character1.say`You don't have enough coins!`
])
]);
Chainable Methods
set<K extends StringKeyOf<T>>
Overload 1 of 2
key: K
- the key of the statevalue: T[K]
- the value of the state
scene.action([
persis.set("coin", 10)
]);
Overload 2 of 2
key: K
- the key of the statehandler: (value: T[K]) => T[K])
- the handler to modify the value
scene.action([
persis.set("coin", (coin) => coin + 10) // coin will increase by 10
]);