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.
Comments
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:
to the links.
The links would look something like this:
Then to see how much time has passed, you would do something like:
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.
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.
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>>.
<<widget "hourpasses">>
<<set $hunger to += 1>>
<<set $fatigue to += 1>>
<<if $hunger gt 10>><<set $playerHealth to -= 1>>
<</widget>>
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'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: You could then call that as part of a simple setter link or a full blown <<click>> if need be. For example: 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.
Also I managed to not close the <<if>> macro.