Howdy, Stranger!

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

Making "time pass" when clicking a link?

So, I'm making my first game in Twine, using Sugarcube. Time will be a part of it: Hunger and tiredness increasing as time goes by, time-of-day sensitive events etc. I'm thinking it would be best implemented as a JavaScript function or a macro. I've been trying to come up with a way of advancing time when the player clicks a link (preferrably with different links advancing time by different amounts), but there doesn't seem to be any support for that. Then I thought I'd use document.GetElementsByClassName() but that didn't work either.

If anyone has any ideas they'd be most welcome.


  • I have a similar function in a game I am working on (my first game!). However, I am using the Harlowe format, which would be different to the Sugarcube one.

    However, just in case you may understand it and be able to port it to SugarCube, I have set a variable $playerTime to 0, which represents minutes, and then I add minutes (e.g. 20 minutes) to the links I want by adding:
    (set: $playerTime to $playerTime + 20)
    to the links.

    The links would look something like this:
    (link:"click here to pass time")[(set: $playerTime to $playerTime + 20)(goto:"next passage")]

    Then to see how much time has passed, you would do something like:
    (print: $playerTime)

    As I said, this would be in the Harlowe format, and I wouldn't know how to write the SugarCube format to do the same. I just thought I would mention it here in case you or anyone else may find it helpful.

  • edited May 2015
    You can do it very simply. For example, I have a variable called $time in my game which corresponds to the different days.

    If someone enters something where time advances to day 2, I just write <<set $time to 2>> at the top of the passage the player enters from the link, and then everything which depends on $time being 2 will show. Since each line of a passage is generated in order, this means even things in the same passage dependent on $time being 2 will show.
  • The solution was actually pretty simple. That's what you get for trying to get things done in the middle of the night with a high fever :P

    What I was trying to get at was that I need a macro which does several things when time passes: The $hunger and $fatigue variables are incremented, if the player is starving $playerHealth is decremented etc.

    For the record this can quite easily be done with a <<click>> macro in Sugarcube: <<click Next step>><<hourPasses>><</macro>>.
  • edited May 2015
    I'd just first make a widget that did:

    <<widget "hourpasses">>
    <<set $hunger to += 1>>
    <<set $fatigue to += 1>>
    <<if $hunger gt 10>><<set $playerHealth to -= 1>>

    Then at the start of the "Next Step" passage simply put <<hourpasses>>

    There's no strict need for on-click events. They still work in this case, but it's technically not needed.
  • @Claretta, your examples have erroneous to operators in them (e.g. $hunger to += 1 is equivalent to $hunger = += 1).

    Claretta's widget idea is a good one. If you aren't using the predisplay/prerender task objects or the PassageReady special passage, then there's no real technical reason not to follow their suggestion.

    That said, if you ever do need to use the task objects or PassageReady, then you likely will need to perform these calculations as part of the click. In which case, I'd probably suggest making a function out of them for ease of calling. For example:
    /* Simple function, requires the argument be a whole number of hours (integer). */
    window.passHours = function (hours) {
    	var sav =;
    	sav.hunger += hours;
    	sav.fatigue += hours;
    	/* maybe preform some logic, like Claretta's example */
    You could then call that as part of a simple setter link or a full blown <<click>> if need be. For example:
    Maybe you should [[search the ice cave|Ice Cave][passHours(2)]].
    Maybe you should <<click [[search the ice cave|Ice Cave]]>><<script>>passHours(2)<</script>><</click>>.
    A question: Are these calculations something that you'll only do on passage transitions (i.e. going from one passage to another)? If so, then leveraging one of the task objects would also allow it to be automated.
  • edited May 2015
    @Claretta, your examples have erroneous to operators in them (e.g. $hunger to += 1 is equivalent to $hunger = += 1).

    Also I managed to not close the <<if>> macro. :)
Sign In or Register to comment.