Howdy, Stranger!

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

SugarCube 2 How to use variables from Sugarcube in JS functions?

edited June 2017 in Help! with 2.0
I want something like this:
<<set $TRACKING["ENEMY_1"] = 40>>
<<set $TRACKING["ENEMY_2"] = 50>>

<<set $enemy["ROOM_1"] = "ENEMY_1">>

<<set $hero_stealth = 10>>
<<set $hero_location = "ROOM_1">>

You can <<link `"hide (" + get_hide_odds() + "%)"` "Hide"`>><</link>>.

window.get_hide_odds = function() {
  var odds = 0;
  stealth = 100 - $TRACKING[$enemy[$hero_location]] + $hero_stealth;
  return odds;
But "$TRACKING[$enemy[$hero_location]]" and "$hero_stealth" doesn't work in JS. They are not defined.
How can I use variables from SugarCube in JS? I need many small functions like get_hide_odds(). And I want use them without arguments like get_hide_odds(enemy,hero). Is it possible? Or I need big structure like "$game" with properties "enemies", "hero", "rooms" etc? And use it like get_hide_odds($game).
Maybe there is better organization of code for my problem (but without fanatism). Thank you.


  • edited June 2017
    So, I found way:
    window.get_hide_odds = function() {
      var odds = 0;
      odds = 100 - State.variables.TRACKING[State.variables.enemy[State.variables.hero_location]] + State.variables.hero_stealth;
      return odds;
    Is it correct? And is it shorter notation?
    If I understand right there is bad idea to write
    var v = State.variables;
    in the beginning of JS. Need I do this in every function?
  • edited June 2017
    Why are you using a function at all, just out of curiosity? Nothing you've written here couldn't be accomplished with a few lines of TwineScript or a widget, and JavaScript methods and such are available from within TwineScript (edit: in SugarCube).

    On top of that, you should probably use the setup object instead of the window object to create your functions if you have a lot.
    GloomDoom wrote: »
    If I understand right there is bad idea to write
    var v = State.variables;
    in the beginning of JS. Need I do this in every function?

    If you're asking if you can store a reference to State.variables, the answer is yes, and I don't think it's a bad idea to do so. To make it global though, it'd probably be best to make a function or something:

    NOTE: This code is for SugarCube 2.18. You didn't specify a format and version, and you should, especially if you're messing with JS.
    setup.getVar = function (varName) {
        return State.variables[varName];
    setup.logLocation = function () {
        var hero_location = setup.getVar('hero_location');
    postdisplay['test'] = function (taskName) {

    At that point, I don't know how much typing you're actually saving yourself, though.

    I think you'd be surprised what functionality you can get out of SugarCube macros alone, though, and I still recommend using widgets over functions if at all possible, you can emulate a return value using temporary variables if you need to, which is what I do:
    ::widgets [widget]
    <<widget 'isBoy'>>
        <<if $player.gender is 'm'>>
            <<set _is to true>>
            <<set _is to false>>
    ::some passage
    <<if _is>>\
        You're a boy-person.
        You are not a boy-person.

    Your example function would look like this using a similar system, and would 'return' the temp variable _odds:
    <<widget 'get_hide_odds'>>
        <<set _odds to 100 - $TRACKING[$enemy[$hero_location]] + $hero_stealth>>

    And then in passage:
    <<get_hide_odds>><<set _odds to 'hide (' + _odds + '%)'>>\
    You can [[_odds|Hide]].
  • Thank you very much for widgets!
    It is exactly what I need. I try to avoid code duplication. so it is reason why I am trying to use JS.
Sign In or Register to comment.