No APIs, no Shortcodes in Sugarcube Tweego Install

Twine Version: 2
Sugarcube 2.36.1

I must be missing something super obvious.

I want to use the State API but have not been able to do so. When I look for the state object in my console I get ReferenceError: Can't find variable: state

I thought the state api was included with Sugarcube. Do I need to do something special to initialize it in my game?

I am also having to write a LOT of HTML using the <HTML> … </html> tags (my content is pretty much 100% HTML at this point) even though the docs say I should not be doing this. None of the built-in macros like << blabla >> do anything, but other shortcodes like !! for H2’s work as intended. (I don’t mind working in HTML as I’m comfortable with it, and even prefer that to a degree; I am making note because the docs say this shouldn’t be the case).

I suspect there is something wrong with my core setup. I am using the Tweego Boilerplate and followed the steps therein. I copied the dist files from the Sugarcube repo into the directory following the docs. tweego --list-formats does indeed show that I can access the correct ver of Sugarcube, which my story is using.

My game compiles fine… I just seem to be missing a lot of functionality and features. Including these wonderful APIs and useful shortcodes.

Edit: tried compiling with NPM per instructions in the Sugarcube repo and then copying over the format, icon, and LICENSE files to Tweego. I see the correct format when I run tweego --list-formats so it’s in there. No errors when compiling; game works fine… just can’t hook up these APIs.

Any ideas?

Thank you!

1 Like

I need to get some more farm work done before it gets dark, but off the top of my head: a) it might be uppercase? State rather than state. I think there was an old api called lowercase state and bits of that may still be hanging around for backwards compatibility… b) SugarCube does some scoping trickery, so if you’re using it from a regular script tag in HTML instead of from Twine’s <<run>> or whatever, you might have to call it SugarCube.State?


Ahh HA!!!

SugarCube.State !

Thank you Josh. You rock.

— Doug

1 Like

WARNING: The global SugarCube object is for debugging purposes only. It neither exposes all relevant APIs, nor is it stable. It’s also unnecessary if you’re running your code within SugarCube’s scope, as you should be.

Use it in your game/story code at your own risk.

Thanks. What do you think the problem may be with my setup? It seems odd that I cannot access any Story() API. Do I need to compile Sugarcube from a different source, link an API from somewhere, or what? I’m following directions in my env as described above.

You shouldn’t need to compile SugarCube at all, unless you’re attempting to run a non-release version. The latest release is available at its website.

Where, specifically, are you placing your .js and .twee files? Global JavaScript libraries go in src/modules. Your local JavaScript files should go in src/scripts. Your Twee files should go in project/twee.

Assuming you’re running npm run build in the project skeleton’s root directory, or using the build.bat file that does the same, the skeleton should write the compiled game to dist/index.html.

There’s absolutely no reason that macros within passages wouldn’t work without something being very, very wrong, so that’s a giant red flag right there. Do you have actual examples of the code, JavaScript and Twee, you’re trying that aren’t working?