Store Data
You can use Persistent to store data in the game context.
type PersisData = {
flag: boolean;
coin: number;
name: string;
// ...
};
const persis = new Persistent<PersisData>("persis", {
flag: false,
coin: 0,
name: "John Smith",
// ...
});
const scene1 = new Scene("scene1");
const char1 = new Character("John Smith");
scene1.action([
persis.set("coin", 50),
char1.say`You have ${persis.get("coin")} coins!`,
]);
Note: The stored content must be serializable, for more information, see Persistent.
You can save the game data including the persistent data to anywhere you want, such as local storage, server, or cloud storage.
const {game} = useGame(); // call this hook in a React component
// ex, if the player click your component
// you can save the game data to local storage
const data = game.getLiveGame().serialize();
console.log(data);
And you can load the game data anytime you want.
const {game} = useGame();
const data = /* ... */;
game.getLiveGame().deserialize(data);
Note: After loading the game data, current game data will be lost.