文档
脚本

脚本

⚠️

本页面正在建设中

⚠️

测试功能,可能会有变动

脚本 允许您执行脚本并与游戏进行交互。

scene.action([
    new Script(({ storable }) => {
        console.log("This script is being executed.");
 
        // 向用户询问价格
        const price = prompt("How much do you want to pay for this item?");
        const namespace = storable.getNamespace("test");
 
        if (!isNaN(price)) {
            namespace.set("price", price);
        }
    })
]);

公共方法

constructor

  • handler: (ctx: ScriptCtx) => void - 当脚本被执行时,将调用此处理程序。处理程序将接收一个 ScriptCtx 对象。
⚠️

如果脚本有任何副作用,处理程序应该返回一个清理函数。
清理函数将在玩家撤销操作时被调用。

new Script((ctx) => {
    const token = setTimeout(() => {
        console.log("This script is being executed.");
    }, 1000);
 
    return () => clearTimeout(token);
})
const script = new Script((ctx) => {
    console.log(ctx.gameState);
});