Number Options
Before this: Fmt
Next steps: Word Forms
Parts that use this: Clock Sign, Race Timer, Round Manager, Obstacle Course, Team Match
A slot can carry an option that changes how its number prints: commas between the thousands, or a fixed number of decimals. Leading zeros have no option, so this lesson builds them by hand, then prints real dates and times.
Contents
chip2 "Counter"
chip "Sign"
button Held -> chip2 addRed
chip2 red -> chip presses
chip big -> decal1 Text
chip share -> decal2 Text
chip both -> decal3 Text
Code
@left in presses: int
@right out big = Fmt("{A:,} points", presses * 125000)
@right out share = Fmt("{A:.2} each", presses / 4.0)
@right out both = Fmt("{A:,.2} coins", presses * 1234.5)
An option goes inside the slot, after a colon.
{A:,}groups the thousands with commas:1,234,567.{A:.2}prints exactly two digits after the point:12.50. Any count up to 6 works.{A:,.2}does both:1,234.50.
presses / 4.0 divides by a number with a point in it, so the result keeps
its fraction: 3 presses give 0.75, not 0.
After three presses, what does each sign show?
Run
- Compile the chip and paste it in-game. Place a Button and three bricks with a Text Decal.
- Paste the counter chip from Interpolation. Wire
the Button’s Held to its
addRedand itsredtopresses. Wirebig,shareandbothto the three Text Decals’ Text. - Press the button three times. The signs read
375,000 points,0.75 eachand3,703.50 coins. Press once more:shareshows1.00, two decimals even when they are zero.
Under the Hood
Open the chip. Each Format String gate still has its option in its
Format String ({A:,} points): the gate applies the option every time the
value changes.
Write options on the lettered slots, as here. Whether they also work on the
numbered form ({0:.2}) has not been checked, so the lettered form is the one
to use. Options need Fmt: a ${} template has nowhere to put them.
Modify
-
Leading zeros. No option pads
7to07, so write a helper that does.mod pad2(n: int) -> string { let s = "0${n}" return s.Substring(s.Length() - 2, 2) } @left in presses: int @right out padded = pad2(presses)moddefines a helper with a name.n: intis its input,-> stringsays it gives back text, andreturngives that text. Each use copies the helper’s gates into the chip (mod). The body puts a0in front, then keeps the last two characters:s.Length()counts the characters, ands.Substring(start, length)takeslengthof them from positionstart. So 7 becomes07, and 42 becomes042and then42. It only works below 100: 100 would come out as00. -
Minutes and seconds. A lap timer: the first press starts it, and every press after that shows the time since the first, as
mm:ss.mod pad2(n: int) -> string { let s = "0${n}" return s.Substring(s.Length() - 2, 2) } @left in pressed: exec var started: int = 0 var elapsed: int = 0 @right out lap = "${pad2(elapsed.Value / 60)}:${pad2(elapsed.Value % 60)}" on pressed { let now = GetUnixTime() if started == 0 { started = now } elapsed = now - started }GetUnixTime()reads the server’s clock as a whole number of seconds (counted from 1 January 1970).if started == 0 { ... }runs its block only on the first press, while nothing is stored yet. Dividing one whole number by another gives a whole number, soelapsed / 60is the full minutes.%gives the remainder: the seconds left over. Wire a Button’s Held topressedandlapto a sign. -
Dates.
FormatDateturns a Unix time into text.@left in pressed: exec var stamp: int = 0 @right out when = FormatDate(stamp.Value, "%Y-%m-%d %H:%M", useUTC = true).Output on pressed { stamp = GetUnixTime() }The sign shows when the button was last pressed. In the format,
%Yis the year,%mthe month,%dthe day,%Hthe hour and%Mthe minute.useUTC = truegives the time in UTC, andfalsegives the server’s local time. The gate also has a Success output,.Success, which is false when it could not format the date.
Make
Make a sign for a parkour course: the first press of a start button stores
the time, and a finish button shows the run’s time as mm:ss, plus the date
and time of the run.
button1 Held -> chip start
button2 Held -> chip finish
chip text -> decal Text
Good to Know
- Store times from
GetUnixTime(), notServerUptime(). Uptime starts again from zero when the server restarts, but avarkeeps its value, so a stored uptime can point into the future.
See String Formatting and Date / time for the reference.
Next: Word Forms.