Howdy, Stranger!

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

How to set variables from within Javascript in Sugarcube 2?

For example, if I want to set or initialize some variable within a script-tagged passage (like StoryInit).

I was thinking something like
State.variables.foo = 'bar';

But of course that's not working.

Comments

  • Because that code is Javascript it will need to be placed within your story's script tagged passage not in StoryInit which is for macro based code. If your story does not have a script tagged passage yet then you can use the Story > New > Script menu items to create one.
  • NOTE: You should never combine special passages with special tags. You will most likely break things in subtle and hard to detect ways by doing so.

    Within the StoryInit special passage:
    <<set $foo = 'bar'>>
    

    Within a script-tagged passage (or Story JavaScript in Twine 2):
    State.variables.foo = 'bar';
    
  • edited September 2016
    Thanks for the help!

    For some reason I thought StoryInit was supposed to be a script-tagged passage. Not sure where I got that idea. So StoryInit should not be a script-tagged passage, and its main use is just to use it for initializing variables via
    <<set $foo = 'bar'>>
    
    Is that right?

    For defining javascript functions, libraries, macros, as well as executing javascript that I want to run once at the beginning of a story, all of that should just be in a single separate script-tagged passage? (For example, if I want to modify the dom in some way, or define sound objects for use with Howler.js?)

    And does all of that really need to be in a single script-tagged passage, or can multiple script-tagged passages be used? I'm writing my story in Twee and using StoryIncludes partly for git, and partly because I like being able to organize things in different files, and having to put all that javascript in a single passage/file is really a bummer from an organizational perspective...
  • […] So StoryInit should not be a script-tagged passage, and its main use is just to use it for initializing variables via
    <<set $foo = 'bar'>>
    
    Is that right?
    It should not be tagged, no.

    Its use is for any kind of pre-start initialization—e.g. initializing variables via <<set>>, setting up audio via <<cacheaudio>>, etc.

    Obviously, it has some crossover with script sections (Twine 1: script-tagged passages; Twine 2: Story JavaScript), since you may do various initialization tasks there as well.

    The biggest differentiation between them is the basic language used. The StoryInit special passage uses SugarCube markup, while script sections use JavaScript—though StoryInit can always run JavaScript via <<script>>. StoryInit is also executed a bit later during the startup sequence.

    For defining javascript functions, libraries, macros, as well as executing javascript that I want to run once at the beginning of a story, all of that should just be in a single separate script-tagged passage? (For example, if I want to modify the dom in some way, or define sound objects for use with Howler.js?)

    And does all of that really need to be in a single script-tagged passage, or can multiple script-tagged passages be used?
    Script sections are a good place for many of those things, yes.

    Since you're using a Twine 1 workflow, however, they are not required to into a single script section (script-tagged passage)—you may split them amongst different script sections. If you do split them up, you must ensure that you do not attempt to reference something which is undefined at that point—i.e. the ordering of your script sections may be important. The ordering gotcha is, actually, why multiple script and stylesheet sections are, generally, frowned upon.
  • edited September 2016
    Thanks for the clarifications, TME--very helpful.

    I understand the potential issue with referencing functions etc. that are not yet defined. Can't this be controlled fairly easily, though, by the order in which files are included in StoryIncludes?

    0hJzPYY.png

    Let's say each of those javascript.twee files contains a single script-tagged passage with some js functions, etc. defined. In the above example, I should be able to reference anything defined in javascript1.twee from javascript2.twee, since js2 is included later, right?
  • That may or may not effect the order that the passages contained in those twee files are added to the HTML file being generated.

    But the order TME is talking about is the order those script tagged passage get processed (converted from passage content into Javascript code and possible executed) when the HTML file is viewed in a web-browser.
  • edited September 2016
    I understand the potential issue with referencing functions etc. that are not yet defined. Can't this be controlled fairly easily, though, by the order in which files are included in StoryIncludes?
    […]
    Let's say each of those javascript.twee files contains a single script-tagged passage with some js functions, etc. defined. In the above example, I should be able to reference anything defined in javascript1.twee from javascript2.twee, since js2 is included later, right?
    First. Didn't you say that you were using Twee? Why a Twine 1 screenshot?

    Anyway. As greyelf noted, it's the order in which they're processed by the compiled story format which is what ultimately matters. There's no one "correct" way of doing this, so different story formats may, or may not, make guarantees about the order of execution. For example, SugarCube does guarantee that the order in which all "code" passages exist within the data store will be the order in which they're processed (by type)—by "code" passages I mean: script, stylesheet, and widget passages.

    That said, the ordering within your files may be helpful in achieving the desired processing order. The sticking point is whether or not the compiler in question makes any guarantees about the ordering of your passages within the data store relative to the way your source was loaded by the compiler.

    This is not an exhaustive list of Twine 1 style compilers:
    • Twine 1: I don't believe that Twine 1 makes any guarantees as to ordering—not 100% sure about that, using StoryIncludes is probably safe.
    • Twee: Twee guarantees that the ordering within the source will be mirrored within the data store. Recent versions only—which are found within the Twine 1 repo, not Twee's own repo. You'll need to keep a Twine 1 install around as well, since Twee depends upon it.
    • TweeGo: TweeGo guarantees that the ordering within the source will be mirrored within the data store.

    EDIT: Some clarifications.
  • Thanks for the help and clarifications greyelf and TME: that helps a lot.

    (as far as Twee vs Twine: I'm writing everything in Twee, but compiling via Twine 1.4, using the StoryIncludes passage. I don't remember why I'm doing this rather than compiling with Twee, though I think it might have had something to do with Twee not seeming to understand the StoryIncludes idea?)
  • (as far as Twee vs Twine: I'm writing everything in Twee, but compiling via Twine 1.4, using the StoryIncludes passage. I don't remember why I'm doing this rather than compiling with Twee, though I think it might have had something to do with Twee not seeming to understand the StoryIncludes idea?)
    You were most likely using a ancient version of Twee. You must be using a recent version of Twee (twee and untwee)—found only within the Twine 1 repo, the Twee repo is completely out of date last I knew.

    You could also try TweeGo, which is a completely standalone Twee-like compiler compatible with your workflow. Like Twine 1 and Twee, it expects to be a sibling of your targets directory.
  • Thanks for the tip!
Sign In or Register to comment.