Skip to content

剧情 API

This content is not available in your language yet.

剧情系统通过 executePluginCommand('scenario', ...) 进行控制,管理故事加载、流程控制、变量和存档。

将编译好的故事文件加载到引擎中。故事文件通常位于 assets/scenario/ 目录下。

executePluginCommand('scenario', {
subCommand: 'addStory',
name: 'main', // 故事名
src: 'scenario/main.json', // 文件路径
});
参数类型说明
namestring故事名称(唯一标识)
srcstring故事文件路径(相对于 assets/
executePluginCommand('scenario', {
subCommand: 'removeStory',
name: 'main',
});
const exists = executePluginCommand('scenario', {
subCommand: 'hasStory',
name: 'main',
});
// 返回 boolean

getStoryList — 获取已加载的故事列表

Section titled “getStoryList — 获取已加载的故事列表”
const list = executePluginCommand('scenario', {
subCommand: 'getStoryList',
});
// 返回 string[]
executePluginCommand('scenario', {
subCommand: 'startStory',
name: 'main', // 故事名
entry: 'chapter1', // 可选:入口点名称
});
参数类型说明
namestring要开始的故事名称
entrystring可选,入口点。未指定时从默认入口开始
executePluginCommand('scenario', {
subCommand: 'terminateStory',
});
executePluginCommand('scenario', {
subCommand: 'nextLine',
});

引擎执行下一行脚本后,会通过事件系统分发 scenariocommandline(命令行)或 scenariotext(文本行)事件。当故事结束时分发 scenariofinished 事件。

通知引擎当前帧已处理完毕,进入等待状态。

executePluginCommand('scenario', {
subCommand: 'setWaiting',
});

引擎进入等待状态后,会分发 scenariowaiting 事件。如果等待被取消(如用户点击跳过),会分发 scenariowaitingcancelled 事件。

一个典型的剧情执行周期:

nextLine() → 引擎执行脚本
→ scenariocommandline / scenariotext 事件
→ 框架处理命令(动画、对话等)
→ setWaiting() 通知处理完毕
→ scenariowaiting 事件
→ 等待用户交互或自动计时
→ nextLine() ... 循环

会话变量仅在当前游戏会话有效,随存档保存和恢复。

// 设置单个变量
executePluginCommand('scenario', {
subCommand: 'setVariable',
name: 'met_alice',
value: true,
});
// 获取单个变量
const val = executePluginCommand('scenario', {
subCommand: 'getVariable',
name: 'met_alice',
});
// 批量设置
executePluginCommand('scenario', {
subCommand: 'setVariables',
variables: {
route: 'alice',
affection: 50,
met_alice: true,
},
});
// 批量获取
const vars = executePluginCommand('scenario', {
subCommand: 'getVariables',
});
// 返回 Record<string, any>

永久变量跨存档持久化,保存在 global_data.json 中。适合记录全局解锁成就、已通关路线等。

// 设置
executePluginCommand('scenario', {
subCommand: 'setPermanentVariable',
name: 'route_alice_cleared',
value: true,
});
// 获取
const cleared = executePluginCommand('scenario', {
subCommand: 'getPermanentVariable',
name: 'route_alice_cleared',
});
// 批量设置/获取
executePluginCommand('scenario', {
subCommand: 'setPermanentVariables',
variables: { route_alice_cleared: true, total_endings: 3 },
});
const all = executePluginCommand('scenario', {
subCommand: 'getPermanentVariables',
});
// 清除所有永久变量
executePluginCommand('scenario', {
subCommand: 'clearPermanentVariables',
});
executePluginCommand('scenario', {
subCommand: 'saveGame',
name: 'slot_1', // 存档名
extra: { // 可选:附加数据
title: '第一章·相遇',
timestamp: Date.now(),
},
});
参数类型说明
namestring存档名(文件名)
extraobject可选,附加的自定义数据

存档文件为 ZIP 格式(.sav),内含:

  • game_data.json — 执行栈和会话变量
  • snapshot.webp — 当前画面截图
  • metadata.json — 存档元数据
  • extra.json — 自定义附加数据
executePluginCommand('scenario', {
subCommand: 'loadGame',
name: 'slot_1',
overwrite: true, // 可选:是否覆盖当前游戏状态
});
参数类型说明
namestring存档名
overwriteboolean可选,是否覆盖当前状态,默认 true

清除执行栈和会话变量,回到初始状态。

executePluginCommand('scenario', {
subCommand: 'resetGame',
});
executePluginCommand('scenario', {
subCommand: 'removeGame',
name: 'slot_1',
});
const saves = executePluginCommand('scenario', {
subCommand: 'getGameList',
pattern: 'slot_*', // 可选:glob 匹配模式
});
// 返回存档信息数组
参数类型说明
patternstring可选,glob 匹配模式,默认返回所有存档