Word Forms
Before this: Number Options
Next steps: Templates Chosen at Run Time
Numbers need the right words around them: 1 apple but 2 apples, 1st but 2nd.
A bool in a slot prints 1 when true, which is rarely what a sign should say.
The Format String gate can pick these words itself, in the template.
Contents
chip2 "Counter"
chip "Sign"
button Held -> chip2 addRed
chip2 red -> chip presses
switch Enabled -> chip isOpen
chip doorText -> decal1 Text
chip appleText -> decal2 Text
chip placeText -> decal3 Text
Code
@left in isOpen: bool
@left in presses: int
@right out doorText = Fmt("Door: {A:Open=Closed}", isOpen)
@right out appleText = Fmt("{A} {A}|plural(one=apple,other=apples)", presses)
@right out placeText = Fmt("You came {A}{A}|ordinal(one=st,two=nd,few=rd,other=th)", presses)
{A:Open=Closed}is an option for a bool: the word before=when the value is true, the word after it when false.{A}|plural(...)is a word form. A slot followed by a bar and a form prints a word picked by the value, instead of the value itself. The form lists words by category:oneis the word for a count of one,otherthe word for larger counts. That is why the template has{A}twice: the first prints the number, the second its word.{A}|ordinal(...)picks an ending for a place:one,two,fewandotherare the categories the gate sorts places into, giving1st,2nd,3rdand4th.
After one press, what do the signs say? After two?
Run
- Compile the chip and paste it in-game. Place a Switch, a Button and three bricks with a Text Decal.
- Paste the counter chip from Interpolation. Wire
the Button’s Held to its
addRed, itsredtopresses, and the Switch’s Enabled toisOpen. Wire the three outputs to the three Text Decals’ Text. - Flip the switch:
Door: Open,Door: Closed. Press the button and watch the words change with the number. - Find out two things the lesson does not tell you. What does
appleTextsay before the first press? What doesplaceTextsay at 11, 12 and 13?
Under the Hood
Open the chip: three Format String gates and nothing else. The words are inside each gate’s Format String, and the gate picks them as the value changes. No compare or select gates do the choosing.
By hand, the plural alone takes a compare and a select:
let word = if presses == 1 then "apple" else "apples". The ordinal would
take a chain of them.
These templates stay real gates even when every value is a constant: the compiler leaves options and word forms for the game to apply.
Modify
-
A form can insert the value itself. This prints
one applefor 1 and3 applesfor 3:@left in presses: int @right out basket = Fmt("{A}|plural(one=one apple,other={A} apples) in the basket", presses)Here the number appears only inside the
otherform, so there is one{A}before the bar. -
Two values with their own words. Add a second Button, counted on the counter’s
blue, oncoins:Fmt("{A} {A}|plural(one=player,other=players) found {B} {B}|plural(one=coin,other=coins)", presses, coins). -
Mix an ordinal and a bool option.
place <= 3is a bool, so{B}can turn it into words:@left in place: int @right out result = Fmt("{A}{A}|ordinal(one=st,two=nd,few=rd,other=th) place. Medal: {B:Yes=No}", place, place <= 3)
Make
Make a door sign that reads The door is open. It has been opened 1 time.,
with the right word for open or closed and for time or times. Count the
openings in a var that a handler adds to each time the switch turns on, as
in Remembering Values.
switch Enabled -> chip isOpen
chip text -> decal Text
Good to Know
- A
boolin a plain slot prints1when true. Use{A:Yes=No}, with your own words, to show it on a sign.
See String Formatting for the reference.
Next: Templates Chosen at Run Time.