Score Card
Before this: Templates Chosen at Run Time, Rich Text
This project uses the whole Templates branch in one template, filled by one gate: a sign with a styled title, numbers with options, the right words for each count, and a status line.
Contents
chip2 "Counter"
chip "Sign"
button1 Held -> chip2 addRed
button2 Held -> chip2 addBlue
chip2 red -> chip hits
chip2 blue -> chip misses
switch Enabled -> chip isOpen
chip card -> decal Text
Code
const CARD = '<size="32"><b><color="ffd040">TARGET RANGE</></></><br><color="ffffff">{A:,} {A}|plural(one=hit,other=hits), {B:,} {B}|plural(one=miss,other=misses)<br>Accuracy: {C:.1}%<br>{D:Open=Closed}'
@left in hits: int
@left in misses: int
@left in isOpen: bool
let accuracy = hits * 100.0 / (hits + misses)
@right out card = Fmt(CARD, hits, misses, accuracy, isOpen)
- The title line is rich text from Rich Text: a
size, bold and a color, each closed with
</>. The color tag before the body is never closed, so it lasts to the end. {A:,}and{C:.1}are number options from Number Options.{A}|plural(...)and{D:Open=Closed}are word forms from Word Forms.CARDis aconsttemplate filled byFmt, as in Fmt.
Before anything is pressed, hits + misses is 0. What does the accuracy line
say then?
Run
-
Compile the chip and paste it in-game. Place two Buttons, a Switch and a brick with a Text Decal.
-
Paste the counter chip from Interpolation. Wire the Buttons’ Held to its
addRedandaddBlue, itsredtohitsand itsbluetomisses, and the Switch’s Enabled toisOpen. Wirecardto the Text Decal’s Text. -
Press each button a few times and flip the switch. The card should read like this after three hits and one miss, with the switch on:
TARGET RANGE 3 hits, 1 miss Accuracy: 75.0% Open
Under the Hood
Open the chip. There is one add, one multiply and one divide for the accuracy, and one Format String gate for the whole card: four gates in all. Everything else (the title, the colors, the words, the options) is text inside the gate’s Format String.
The accuracy needs no check for zero shots. Dividing by zero gives 0, as in
Numbers, so a fresh card reads Accuracy: 0.0%.
The template must stay in one Fmt. Built with .. instead, each piece
would be its own gate, and the options and word forms would not work, because
only the Format String gate reads them.
Modify
- Add a best streak. Give the chip a third pin,
streak, add<br>Best streak: {E}to the template, and passstreakas the fifth value. - Color the accuracy by how good it is. Pick the color with a lookup
Fmtfrom Templates Chosen at Run Time, usingclamp(hits * 3 / (hits + misses), 0, 2)as the position, and pass it intoCARDas one more slot:<color="{F}">Accuracy: {C:.1}%. The position must be a whole number:hits * 3 / (hits + misses)divides twoints, which drops the fraction, whileaccuracyis a decimal and would build a format like{1.47}that picks nothing. - Give the card a place line:
You are {G}{G}|ordinal(one=st,two=nd,few=rd,other=th), withGfrom a second counter chip’sred. A template holds at most seven values,AtoG, so this one is now full.
Make
Make a shop sign for a vending machine. It shows the item name in a large
colored title, the price with thousands grouped (1,250 coins), how many are
left with the right word (1 can left, 3 cans left), and Sold out or
In stock from a bool. Use one const template and one Fmt.
chip2 "Counter"
chip "Shop"
button Held -> chip2 addRed
chip2 red -> chip remaining
switch Enabled -> chip inStock
chip card -> decal Text
Good to Know
- A template holds seven values at most,
{A}to{G}. A card that needs more must be split into twoFmts joined with... - A misspelled option is not an error. The compiler cannot check what is inside a template, so check the sign itself after every change.
- Text that comes from a player must never be part of the template itself: pass it in a slot, escaped, as in Safe Text.
Next: pick a lesson from the tree on the Learn page.