Howdy, Stranger!

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

Javascript with Sugarcube

Hi. I'm new to Twine and I've spent the last three hours trying to find something in the documentation or forum that explains my question, but I've completely failed. I'm sure this has been answered somewhere before. I'm sorry for asking again.

I want to run loops manipulate arrays in javascript using functions, and return those values for use in my normal twinecode. I really only need like three java functions for the game I'm planning on setting up, but I can't figure out how to call them from the Edit Story Javascript area. I read that for Harlowe I needed to use a global object, so I just sort of did that here for Sugarcube. IDK what I'm doing at all.

Can anyone explain to me how to make the following function run in Twine? Thank you so much for your time.
if (! window.rollevent) {
	window.rollevent = function() {
	  var sum = 0;
	  for ( i = 0; i < events_All.length; i++) {
		  var sum = sum + window['event_'+events_All[i]]
	  };

	  var i = 0;
	  var total = 0;
	  var random =  Math.floor( Math.random() * sum );
	  while (total < random) {
		  var total = total + window['event_'+events_All[i]];
		  var choice = events_All[i];
		  i++;
	  };
		return choice;
	}
}

Comments

  • edited March 2017
    I can see that you're returning a value, but I'm not sure whether you intend for that value to be stored for later or if it is meant to be stored for later. In any case, you can create new SugarCube macros that directly run JavaScript, here's a generalized example of what you can do with it:
    Macro.add("myCustomMacro", {
      handler: function () {
        var storyVariables = State.variables; //If you can directly read/write story variables using this.
    
        var text = "this could easily be the result of some function call";
        
        storyVariables.exampleText = text; //this saves text to a variable called $exampleText
        
        $(this.output).wiki(text); //this prints text where ever you called the macro
      }
    });
    

    You would these call this macro by putting <<myCustomMacro>> somewhere in a passage.

    After this macro run, you would have a story variable called $exampleText that could be printed out using
    <<= $exampleText>>
  • Rokiyo wrote: »
    ... but I'm not sure whether you intend for that value to be stored for later or if it is meant to be stored for later...
    Wow, not my finest moment, and it's too late to go back and edit it.

    I meant to say I'm not sure whether you intend for that value to be stored for later or if it is meant to be printed out into the passage.

  • Puppeteer wrote: »
    Can anyone explain to me how to make the following function run in Twine? Thank you so much for your time.
    Assuming you've placed that code within your Story JavaScript, you don't need to do anything special to invoke it within TwineScript. For example, it could be as simple as the following:
    rollevent();
    


    That said, the function causes no side-effects and only returns a value, so the the real question here—somewhat clumsily introduced by Rokiyo—is what do you want to do with its return value?
  • Thanks so much, guys. I got it to work. The mechanics of the script itself is something I can handle, I just couldn't figure out the TwineScript side. It turns out my mistake was trying to call it outside of a <<set>> command.
  • edited March 2017
    FYI there is also a <<run>> command that does exactly the same thing but is, uh, intended for that sort of thing, I guess.
Sign In or Register to comment.