HTML Twine - Help with functions

tgroxi

New Member
Apr 26, 2019
3
1
Hello,
im new to twine, ive tried my first steps with twine_1.4.2 and sugarcube. But I cant find way to create own functions.
for example:
setupTime(30)

function setupTime( $addTime){
$timeMinutes = $timeMinutes +$addTime
........
}

Is there no way to create own functions?

sry im realy new to that stuff.
 

danasavage

Member
Game Developer
Mar 9, 2020
147
273
Twine uses pure javascript and then a layer of 'twinescript' on top of that. It depends how deep you wanna go!

If you just want to make your own simple function like time, sticking to Twine language, you'd most likely create a "widget" - look up how to do this on the web! But basically it's a way of wrapping up a number of twine functions in one that you could then call at any point you wanted.

Or if you knew Javascript and wanted to go really deep, you could write your own functions in pure Javascript and then wrap those in 'Macros' which - as far as I understand it - are similar to widgets but ... yeah. Like i said, are just javascript. Again, there's a bunch of documentation online and advice also depends on what version you're using (Harlowe? Sugarcube?) ...

The twinery.org forums are pretty useful, there's an active subreddit at r/twinegames and I'd also recommend searching on YouTube too.

Oh, and for a couple of repositories for pre-made functions that can take your twine game up a notch, these are both really good too:




They might also be useful for analyzing the source code to work out how they're doing what they're doing - as it sounds from your question like you already have some programming knowledge? Anyway, there's tons of info out there to help you find what you need. Good luck! :)
 
  • Like
Reactions: tgroxi

HiEv

Member
Sep 1, 2017
384
785
im new to twine, ive tried my first steps with twine_1.4.2 and sugarcube. But I cant find way to create own functions.
for example:
setupTime(30)

function setupTime( $addTime){
$timeMinutes = $timeMinutes +$addTime
........
}

Is there no way to create own functions?
Yes, but in Twine/SugarCube it's easier to create a for that. For example, you could create a passage for your widgets, add "widget" and "nobr" tags, and put this code in it:
Code:
<<widget "setupTime">>
    <<set $timeMinutes += $args[0]>>
<</widget>>
Now, in your Twine passages, you could just do this:
Code:
<<setupTime 30>>
and that would add 30 to $timeMinutes.

If you truly want to use a JavaScript function, then you could put this in your JavaScript section:
JavaScript:
setup.setupTime = function (addTime) {
    State.variables.timeMinutes += addTime;
};
That adds a setupTime() function to the SugarCube setup object, which you could then call from your code.

Then, in your passages, you'd do this instead:
Code:
<<run setup.setupTime(30)>>
You can see why the <<widget>> method is a bit easier to use.

Hope that helps! :)
 
  • Like
Reactions: SerokMinmas

tgroxi

New Member
Apr 26, 2019
3
1
thx widget is that what i was searching, i dont want to use javascript for first