0 votes
by (180 points)
Hello Community

I have just started to use Twine and Sugarcube and so far the giant Sugarcube Wiki was a really good help. I have created a lot of code that is working perfectly fine in Twine/Sugarcube, and I actually want to use it more then once. For that I thought to myself that I could write this code into a Java Script Function and call it whenever I need it. Unfortunately that didn`t work out as good as I hoped to. So my questions so far are:

- Is it possible to call a Javascript Function in Sugarcube while giving the Java Script Function three variables to work with?

- Can a Javascript function return more then one value back to Sugarcube?

- If I just want to write a super simple function, where one variable gets pushed to a java script function and the java script function will return a single variable, how can I do that?

- Are there different kind of functions I can use? I think I have read the terms "global" and so on.

- Finally is there a guide or a wiki or something similiar for this matter, as I found it super hard to even find a little bit of info about it.

1 Answer

+1 vote
by (159k points)
selected by
 
Best answer

 Is it possible to call a Javascript Function in Sugarcube...

Yes, and the story format includes many of its own JavaScript functions that you can use.

...giving the Java Script Function three variables to work with?

It is unclear if by "variables" you mean: function arguments; or story variables; but either way you can pass as many arguments to a JavaScript function as it is setup to recieve, and even if you pass in more than that those values are accessible via the special arguments object.

Can a Javascript function return more then one value

No, a function can only return a single value. However that single value can be an instance of an 'collection' type object (like an Array, a Map, or a Generic Object), which in turn can contain multiple values.

Any JavaScript code you place within your project's Story Javascript area is executed within what's know as a Private Scope, the same is true for any JavaScript code you execute using the <<script>> macro, This means that any functions you define within those areas are unavailable to be used outside of those area, unless you manually change those functions scope from Private to 'Global' like.

There are a number of ways you raise the scope of such functions, two of them being:

1. Define the function on SugarCube's special setup object.

This object is available 'globally' from most places you would call JavaScript functions from, however you can't access it from the on<event-name> attibutes of a HTML element tag.

setup.hello = function (name) {
	return "Hello ' + name + ', how are you?';
};

note: You need to include the setup object's name when you call such a function..

<<= setup.hello('Jane')>>


2. Define the function on the web-browser's special window object.

This object is available 'globally' anywhere you can execute JavaScript, including from the on<event-name> attibutes of a HTML element tag.

window.hello = function (name) {
	return "Hello ' + name + ', how are you?';
};

note: You don't need to include the window object's name when you call such a function, but you can if you want to.

<<= hello('Jane')>>

<<= window.hello('John')>>

warning: Web-browser developers (and some third-party JavaScript library developers) also use this object to contain their function / property definitions, so some care needs to be taken when defining your own because you may be overwriting someone else's. This is why we generally recommend using the setup object instead, unless you need to use the function from an on<event-name> attibutes of a HTML element tag.

by (180 points)
Thank you very much for your quick reply. I managed to do it with your help :)
...