Text Values
Before this: Remembering Values
Next steps: Taking Text Apart, Rich Text
Signs, chat lines and labels all carry text. In Wirescript a piece of text is
a string, and it moves along wires like any other value. This lesson builds
a shop sign that two buttons change, and a light that shows whether the sign
has anything on it.
Contents
button1 Held -> chip away
button2 Held -> chip back
chip text -> decal1 Text
chip count -> decal2 Text
chip lit -> light Enabled
Code
const AWAY = "Back in 5 minutes"
@left in away: exec
@left in back: exec
var note: string = ""
@right out text = "Shop: " .. note.Value
@right out lit: bool = note.Value
@right out count = "${note.Value.Length()} characters"
on away {
note = AWAY
}
on back {
note = ""
}
Text goes between quotes. "" is the empty string: text with nothing in it.
var note: string = "" is a variable that holds text and starts empty.
.. joins two pieces of text into one, so "Shop: " .. note.Value is the
words Shop: followed by whatever the note says.
note.Value.Length() counts the characters in the note.
lit is declared bool, but it is given a string. Where a bool is wanted,
a string reads as false when it is empty and true when it holds anything at
all.
What does each output show before any press, after pressing away, and after
pressing back?
Run
- Compile the chip and paste it in-game. Place two Buttons, two bricks with a Text Decal each, and a Point Light.
- Wire one Button’s Held to
awayand the other’s toback. - Wire
textto one Text Decal’s Text andcountto the other’s Text. Wirelitto the Point Light’s Enabled. - At first the sign reads
Shop:, the count reads0 characters, and the light is off. - Press
away. The sign readsShop: Back in 5 minutes, the count reads17 characters, and the light comes on. Pressbackand all three return to how they started.
Under the Hood
Open the chip. note is a Variable gate, and each handler is one Set
Variable that writes a fixed text into it.
textcomes from an Append String gate, the game’s join gate. Its inputs areShop:and the variable’s value.countcomes from a Get String Length gate feeding a Format String gate. That is the${...}from Remembering Values: one gate that fills values into a template.litcomes from a Not Equal gate that compares the note with"". You did not write it: the compiler adds it wherever a string is used as abool.
When every piece of a join is fixed text, the compiler joins it for you and no Append String is placed for it. Only a join with a value that can change needs a gate.
Quotes and escapes
A string can use double quotes or single quotes. They mean the same thing, so pick the one that saves you work. Single quotes let the text hold double quotes as they are:
'Ring the "bell"'
"Ring the \"bell\""
Both lines are the same text. A backslash in front of a character is an
escape: \" is a double quote inside double quotes, \' a single quote
inside single quotes, \\ a backslash, and \$ a dollar sign that does not
start a ${...}. Escape Sequences lists
them all. For a new line on a sign, use the <br> tag from the
Rich Text lesson.
Modify
-
Change
AWAYto'Ring the "bell"'. Compile and pressaway: the sign shows the quotes. How many characters does the count show now? -
==compares two strings and gives abool. Add a pin that is on only while the note says exactlyAWAY, and wire it to a second light:const AWAY = "Back in 5 minutes" @left in away: exec @left in back: exec var note: string = "" @right out text = "Shop: " .. note.Value @right out isAway = note.Value == AWAY on away { note = AWAY } on back { note = "" }!=is the opposite: true while the two texts differ. -
Use one button for both jobs. Inside a handler,
if noteis the same test aslit: true while the note is not empty.const AWAY = "Back in 5 minutes" @left in pressed: exec var note: string = "" @right out text = "Shop: " .. note.Value on pressed { if note { note = "" } else { note = AWAY } }Open the chip: the
ifis a Branch gate, and its condition is the same Not Equal against""that the compiler added forlit.
Make
Make a doorbell sign: pressing a Button shows Ding! on a Text Decal for
three seconds, then clears it, and a light stays on while the sign has text.
await Sleep(_, delay = 3.0) from Pure and Exec does
the waiting.
button Held -> chip ring
chip text -> decal Text
chip lit -> light Enabled
Good to Know
Length()counts characters as the game sees them (code points), so★is 1. Some emoji are several code points joined together: a figure with a skin tone and a gender sign counts 5, though it draws as one picture.- Only the empty string is false.
" ","0"and"false"all hold something, so they read as true. - An input pin that nothing is wired to reads as empty text.
Next: Taking Text Apart.