I would love if one of you guys could give me a step by step introduction of how to make narrat call a funktion from a typescript file. If its not to much work.
Can you please update the title of this post to be more representative of the actual question, so when people search for narrat solutions they can find this thread in the future?
For the answer: Basically all you need is to make a narrat plugin. so for example in index.ts you can do that:
function myCustomCommand(arg1: string, arg2: number) {
return `This is a custom command with arguments: ${arg1} and ${arg2}`;
}
class MyCustomPlugin extends NarratPlugin {
customCommands: CommandPlugin<any>[];
constructor() {
super();
this.customCommands = [
CommandPlugin.FromOptions<{
firstArgument: string
otherArgument: number;
}>({
keyword: "command_name",
argTypes: [
{ name: "firstArgument", type: "string" },
{ name: "otherArgument", type: "number" },
],
runner: async (ctx) => {
const { firstArgument, otherArgument } = ctx.options;
return myCustomCommand(firstArgument, otherArgument);
},
}),
];
}
}
This plugin will add the command command_name
to narrat, and the runner
function is what happens when it’s called.
You can put that plugin and the js code directly in index.ts if you want, or put them in another file and import them into your index.ts, as you would any other code.
Then in the part where narrat gets initialised at the end of index.ts, you just need to register the plugin:
window.addEventListener("load", () => {
registerPlugin(new MyCustomPlugin()); // Add this
startApp({
debug,
logging: false,
scripts,
config,
});
});
Then in narrat you can use your command, for example:
test_command:
set data.result (custom_command "hello" 12)
log $data.result
2 Likes