Save
In scripts, we can retrieve the story save data through functions, which records the story path and story data. The path is automatically generated by the system, while the data needs to be handled by the developer.
Story Data
Story data is crucial for various logical judgments. It is located in save.data. You can set the initial values in project.initSaveData and modify them in functions.
For example, let’s set the initial save data as follows:
{
"me": {
"title": "Xiao Ming",
"money": 529900,
"faceScore": 999999999999,
"cp": 500,
"mate": null
},
"NPCList": [
{
"title": "Liu Ruyan",
"money": 10000000,
"faceScore": 99999999999,
"cp": 10000
},
{
"title": "Ruhua",
"money": 300,
"faceScore": 5,
"cp": 9999999999
}
]
}In a certain event of a scene, we decide to sell a pill to Liu Ruyan, causing changes in assets and Liu Ruyan’s combat power:
export default {
...otherParameters(ignore),
/**
* Event Execution
*
* @param {object} save Save - The story save data
* @param {object} story Story - The story object, which can be used for prompts, pop-ups, and other modifications to the story
* @param {object} loader Loader - The Vue node of the event loader
* @return {Promise<Place|yinObject|undefined>}
*/
run(save, story, loader) {
console.log('Save:\n', save)
console.log('Story:\n', story)
const me = save.data.me, liuruyan = save.data.NPCList[0]
// Increase my assets by 100
me.money += 100
// Decrease Liu Ruyan’s assets by 100
liuruyan.money -= 100
// Increase Liu Ruyan’s combat power by 10000
liuruyan.cp += 10000
// At this point, the event execution ends. Since there is no new scene address, the next scene remains the target scene of the event.
}
}Scene Switching
The story path is recorded in save.path as an array. The save object includes two built-in functions to edit this path array. Unless you know what you’re doing, it’s best not to modify the path array directly.
Load Scene
Pass a scene as the new scene and its parent scene (usually the current scene) to create a path index and loop check. After modifying the path, overwrite the story data.
Path Loop
The story path will not create loops:
- If the passed scene is already in the story path, the path is truncated, but the
story datais overwritten. - If the passed parent scene is already in the story path, the path is truncated from the parent scene, and the new scene is added.
export default {
async run(save, story) {
// Normally, returning the scene address will enter the new scene.
// If the new scene is not a child of the current scene,
await save.load('New Scene Address', 'Parent Scene Address')
}
}Go To Scene
Go to a previously visited historical scene. The system will rebuild the path index to the last entry of that scene and restore the story data from that time.
export default {
async run(save, story) {
// Travel back in time to a past scene.
await save.to('Scene Address')
}
}Auto-Save
The save will be automatically updated after a successful scene switch.
Basic Functions
With Yin.Object, the save object includes some additional functions and features that might make the story more interesting.
export default {
async run(save, story) {
// Directly save this save data
await save._save()
// Delete this save data, e.g., expert mode where death results in deletion.
await save._delete()
// Reload the last saved data
await save._reload()
function do_some_thing() {
// Do something
}
// Attach some events to the save data
// Different devices can open the same save data simultaneously, and the story will sync automatically across devices.
// The following events will trigger on all devices.
// Attach a save update event
save._on('update', do_some_thing)
// Attach a save read event
save._on('read', do_some_thing)
// Attach a save delete event
save._on('deleted', do_some_thing)
// Remove attached events
save._removeEvent('update', do_some_thing)
}
}