Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Interchange Javascript and Twine 2.0.3 Sugarcube 'web storage' variables

Since both Javascript and Sugarcube's <<remember>> variables are supposed to use the web storage, I figured I would be able to somehow retrieve a Twine variable $var (set using the macro <<remember $var = 2>>) using the javascript code...
localStorage.getItem('var')
or...
localStorage.getItem('$var')
...neither of which are working for me. Is there another method made available for such an exchange?

Ultimately, I wish to be able to feed a Twine story data from a database via PHP script and JavaScript and then retrieve player decisions in the reverse direction, storing them in a database. Though I have been successful with plain html documents in doing this, I have yet to figure out how to using a Twine story. As of yet, my only options for controlling story flow have been to generate conditional statements based off of variables set using the Sugarcube macros.

1. How can I exchange the JavaScript and Twine web storage variables?
2. Is it possible to have JavaScript contained in a <<script>> tag to provide control flow systems without directly modifying the html content of document elements? Or would I need to simply rely on the macro system?

Comments

  • After investigating further, I believe the solution I'm looking for will mean simply writing my own macros using JavaScript which shouldn't be too complicated.
  • You may find the Macro API documentation and the standard macro library (macroslib.js) useful.

    As to why your first attempt failed.  SugarCube's storage for each story is prefixed (first by a slugified version of the story's title and secondly by subsystem) and compressed to save space.  You're probably better off doing your initial DB load simply as a section of the Story JavaScript, or as a macro if you don't plan on doing all of your loading at startup, and DB storing as a macro.

    Also, the following undocumented static Wikifier methods may help: (they operate on the currently active state)

    // Returns the value of the given story $variable
    Wikifier.getValue(storyVariable);

    // Sets the given story $variable to the given value
    Wikifier.setValue(storyVariable, value);

    // Examples:
    Wikifier.getValue("$foo");
    Wikifier.setValue("$foo", "bar");
    Wikifier.setValue("$foo", true);
    Wikifier.setValue("$foo", 42);
    Wikifier.setValue("$foo", someLocalVariable);
Sign In or Register to comment.