0 votes
by (130 points)

I made a clock.

The variable $time is in minutes

<<set $time to 1440>>

<<if $time > 1440>>
<<set $et to $time - 1440>>
<<set $time to 0 + $et>>
<<endif>>

<<set $hr to Math.trunc($time/60)>>

<<if $time < 60>>
<<set $hr to 12>>
<<endif>>

<<set $min to $time%60>>

<<if Math.trunc($min/10) < 1>>
<<set $min to "0" + $min>>
<<endif>>

<<if $time < 720>>
<<set $ampm to "am">>
<<set $clock = $hr + ":" + $min + $ampm>>

<<elseif $time == 1440>>
<<set $hr -= 12>>
<<set $ampm to "am">>
<<set $clock = $hr + ":" + $min + $ampm>>

<<elseif $time > 780>>
<<set $hr -= 12>>
<<set $ampm to "pm">>
<<set $clock = $hr + ":" + $min + $ampm>>

<<elseif $time >= 720>>
<<set $ampm to "pm">>
<<set $clock = $hr + ":" + $min + $ampm>>

<<endif>>

Might have made it more difficult than it should have been. I am not very good in coding. 

The problem that I am facing is that I am trying to add time after the player clicks on sleep

<<click "Wake up">><<set $time += 5>><<goto [[Start of a new day]]>><</click>>

It still shows the time that was set above which is 12:00 am and not 12:05 am

I have no idea what went wrong.

1 Answer

+1 vote
by (44.7k points)

If "<<set $time to 1440>>" is at the start of your clock code, then that will keep setting the time back to midnight.

Take that line out of your clock code, put it in your StoryInit passage, and then it should work.

by (130 points)
The thing is, that is the init. Should I have made another passage for clock?

 If so, how do I implement it to the story?

Also, what should be in the init and what shouldn't be in the init?

Sorry for asking so many questions, I started using twine two days ago and am not too sure what to do and not to do.

Thank you for your time
by (44.7k points)

The StoryInit passage is for setting variables and running any code that needs to be done before your game begins.  Things that you need to use repeatedly should either be made into widgets or macros, or you could put them in special passages which get triggered on each passage transition.

If your above clock code is only in the StoryInit passage, then it will only get triggered once at the very beginning of the game, which explains why the $clock variable is never changing.

If you're displaying the time on the UI bar through the StoryCaption special passage, then that clock code (minus that first line) should probably also be in the StoryCaption passage before the part that displays the value of $clock.

Also, I should note that you can use the JavaScript Date object to handle time a bit more easily.  See here for some SugarCube time sample code showing how to do that.  (Click "Jump to Start" in the UI bar to see other sample code there.)

Hope that helps!  :-)

...