Twine Version: 2.36.1
I’m just getting started with Tweego and SugarCube. I have a basic npm project set up using esbuild and typescript and am just testing out a few simple things to get my bearings. At the moment, I’m trying to define a custom macro from test.tsx:
SugarCube.Macro.add("mymacro", {
tags: null,
skipArgs: false,
handler: function () {
this.output.append("test output")
},
})
Esbuild puts this in out.js, which gets picked up by tweego and shows up in the index.html
story script element like this:
(() => {
SugarCube.Macro.add("mymacro", {
tags: null,
skipArgs: false,
handler: function() {
this.output.append("test output");
}
})();
The problem is that the userscript runs before the Sugarcube object has been fully initialized, and Sugarcube.Macro.add is still undefined, causing an exception.
If I wrap the entire init block in $(document).on(":storyready", function(ev) { .... }
then the macro registration works fine. But the active passage gets rendered before the macro has been properly registered regardless, so I see macro <<mymacro>> does not exist
when I view index.html in Chrome.
Only on the initial passage though. If I navigate to some other passage that also uses the custom macro, then it works. How do I ensure my userscript waits until after SugarCube is fully initialized, but also gets to register macros before passage rendering?