jooJ
Denoβ€’3y agoβ€’
3 replies
joo

'Spawn' multiple promises from the same module

I'm trying to use Deno for scripted npcs in a game, the main issue that the npc has to await user action so I've used a mpsc Channel for this with an async fn op which works fine. However since the npcs exists on the server side I need to be able to call the same npc multiple times on the same runtime.

My current test script looks like this
// actor would be the current actor object
async function npc_action_1000(actor) {
    Deno.core.print("Started script for player " + actor + "\n");
    let action = await Deno.core.ops.op_wait_action(actor);
    if(action === "0") {
        Deno.core.print("First selection\n");
    } else {
        Deno.core.print("Other selection\n");
    }
}

globalThis.npc_action_1000 = npc_action_1000;


The problem I'm facing right now, I'd like to load the module once, remove the await in the end and then spawn multiple npc_actions while the event loop is running. So far I was only able to get a Local reference to the function, but when I call It I can no longer run the ev loop because the scope references the runtime.

What would be the best way to approach that?
Was this page helpful?