3. Time
Goal
Show sentences at different moments, and ask the viewer a question.
New in this chapter
2s,150msform value-duration spec/01 §1.5after 2sform after spec/07 §7.42stype time spec/04 §4.1sim.StopAtpackage std/sim std/README.md § std/simtime.Nowpackage std/time std/README.md § std/time
Steps
So far everything happened at the first instant. A Synod program runs on a simulated clock, and after 2s runs its block when that clock reaches two seconds. 2s is a duration, a value of type time; ms and s are the two units. We take the program of chapter 2 and move its last caption later:
Step 1
# package main
use std/lesson
use std/text
# Say hello, then work out the bill.
define main
set apples to 3
set price to 4
set tip to 1
set coupon to 2
set people to 2
set total to ((apples times price) plus tip)
set share to ((total minus coupon) divided by people)
lesson.Note "Hello, Synod"
lesson.Note (text.Join {"Total: ", (text.From total)})
after 2s
lesson.Note (text.Join {"Each pays ", (text.From share)})
time.Now is the current simulated time. Put it in the caption to see the clock:
Step 2
# package main
use std/lesson
use std/text
use std/time
# Say hello, then work out the bill.
define main
set apples to 3
set price to 4
set tip to 1
set coupon to 2
set people to 2
set total to ((apples times price) plus tip)
set share to ((total minus coupon) divided by people)
lesson.Note "Hello, Synod"
lesson.Note (text.Join {"Total: ", (text.From total)})
after 2s
lesson.Note (text.Join {"At ", (text.From time.Now), " each pays ", (text.From share)})
A caption can also ask. lesson.Ask pauses the show and asks the viewer to predict what comes next, before it happens:
Step 3
# package main
use std/lesson
use std/text
use std/time
# Say hello, then work out the bill.
define main
set apples to 3
set price to 4
set tip to 1
set coupon to 2
set people to 2
set total to ((apples times price) plus tip)
set share to ((total minus coupon) divided by people)
lesson.Note "Hello, Synod"
lesson.Note (text.Join {"Total: ", (text.From total)})
after 2s
lesson.Note (text.Join {"At ", (text.From time.Now), " each pays ", (text.From share)})
after 4s
lesson.Ask "Who pays the tip?"
Give the viewer choices with choosing and a list of two to six texts:
Step 4
# package main
use std/lesson
use std/text
use std/time
# Say hello, then work out the bill.
define main
set apples to 3
set price to 4
set tip to 1
set coupon to 2
set people to 2
set total to ((apples times price) plus tip)
set share to ((total minus coupon) divided by people)
lesson.Note "Hello, Synod"
lesson.Note (text.Join {"Total: ", (text.From total)})
after 2s
lesson.Note (text.Join {"At ", (text.From time.Now), " each pays ", (text.From share)})
after 4s
lesson.Ask "Who pays the tip?" choosing {"Ana", "Ben"}
Last, sim.StopAt sets the moment the run ends at the latest. This run has nothing left to do after four seconds, so it ends then; if you set sim.StopAt 3s, the question never appears:
Step 5
# package main
use std/lesson
use std/sim
use std/text
use std/time
# Say hello, then work out the bill.
define main
set apples to 3
set price to 4
set tip to 1
set coupon to 2
set people to 2
set total to ((apples times price) plus tip)
set share to ((total minus coupon) divided by people)
lesson.Note "Hello, Synod"
lesson.Note (text.Join {"Total: ", (text.From total)})
after 2s
lesson.Note (text.Join {"At ", (text.From time.Now), " each pays ", (text.From share)})
after 4s
lesson.Ask "Who pays the tip?" choosing {"Ana", "Ben"}
sim.StopAt 8s
What to watch
Captions arrive at 0s, 2s and 4s. At 4s the player stops and shows the question with its two buttons. The clock is simulated: the run does not wait two real seconds, and running it again gives exactly the same events. This is what later lets you replay a failure.
Exercises
- Change
after 2stoafter 1500ms. What does the caption say?
Expected
It appears at 1500ms and reads At 1500ms each pays 5.5.
- Set
sim.StopAt 3s.
Expected
The run ends at 3s, so the question at 4s never appears.
- Remove
choosing {"Ana", "Ben"}.
Expected
The question is shown without buttons; the viewer just thinks of an answer.