Documentation
Script

Script

⚠️

This page is under construction.

⚠️

Beta feature, subject to change.

Script allows you to execute scripts and interact with the game.

scene.action([
    new Script(({ storable }) => {
        console.log("This script is being executed.");
 
        // ask the user for input
        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);
        }
    })
]);

Static Methods

execute

Creates a new Script instance with the provided handler and returns it as a proxied object.

  • handler: ScriptRun - The script handler function to execute
  • Returns Proxied<Script, Chained<LogicAction.Actions>> - A proxied Script instance
const script = Script.execute(({ storable }) => {
    console.log("Script executed via static method");
    const namespace = storable.getNamespace("test");
    namespace.set("value", 42);
});
 
scene.action([
    script, // add the script to the story
]);

Public Methods

constructor

  • handler: (ctx: ScriptCtx) => void - When the script is executed, this handler will be called. The handler will receive a ScriptCtx object.
⚠️

The handler function should return a cleanup function if it has any side effects.
The cleanup function will be called when the player undo the action.

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);
});