Converting Values
Before this: Vectors and Colors
Next steps: Lists
Values often need to change type: a whole number into a float so a divide
keeps its fraction, a float back into a whole number, a number into text.
Some conversions happen by themselves. For the rest, as asks for one.
Contents
- Code
- Run
- Under the Hood
- Conversions that happen by themselves
- What
asconverts any- Modify
- Make
- Good to Know
button Held -> chip roll
chip text -> decal Text
Code
@left in roll: exec
var total: int = 0
var rolls: int = 0
var last: int = 0
let average = total.Value as float / rolls.Value
let nearest = round(average) as int
@right out text = "Rolled ${last.Value}. Average ${average}, about ${nearest}"
on roll {
last = Random(1, 6)
total += last
rolls += 1
}
Each press rolls a die. Random(1, 6) gives a whole number from 1 to 6, and
runs as a step in the handler like a variable write. The handler adds the
roll to total and counts it in rolls.
value as T converts a value to type T. total.Value as float turns the
total into a float before the divide, so the average keeps its fraction.
round(average) rounds to the nearest whole number, but its answer is still a
float. as int makes it an int.
After rolling a 3 and then a 4, the average shows its fraction. What would it
show without as float?
Run
- Compile the chip and paste it in-game. Place a Button and a brick with a Text Decal.
- Wire the Button’s Held to
roll, andtextto the Text Decal’s Text. - Press a few times. The average moves toward 3.5 as the rolls add up, and
aboutshows the nearest whole number.
Under the Hood
Open the chip. Each as is one Convert gate: one before the Divide, one
after the Round. The text needs no Convert of its own: ${} builds the text
with a Format String gate, which writes any value it is given.
Conversions that happen by themselves
boolto a number.trueis 1 andfalseis 0, as in True and False.intandfloat. Mixing them in arithmetic gives afloat, as in Numbers.- Anything into text inside
${}. - Text to
bool, where a bool is wanted: text istruewhen it is not empty. The compiler checks!= ""for you.
Each press shows or clears a message, and the light is on while there is one.
The chip has a Not Equal gate between the message and lamp, comparing
it with empty text.
What as converts
bool,int,floatandstringinto one another.- A
floatto anintdrops the fraction toward zero:2.7 as intis 2, and-2.7 as intis -2. Useroundfirst to get the nearest instead. - A number to a
boolistruewhen it is not zero. - Text to a number reads the number at its start:
"42abc" as intis 42, and"abc" as intis 0. - A
vector,rotatororcolorinto text:X=1.000 Y=2.000 Z=3.000for a vector.
A conversion the game cannot make, such as a vector into a number, would only
give the target type’s empty value, so the compiler warns (WS109) instead.
The full list is in Type Conversion.
any
any is a type that stands for every type. A pin declared @left in probe: any accepts whatever is wired to it, and probe as string shows it as text:
@left in probe: any
@right out text = probe as string
any throws the type away, so the compiler can no longer check what you do
with the value. A variable can hold any too, and keeps whatever kind of value
was stored last. To use it as a number again, ask for its type with
getType(). Inside the if, the compiler knows held is an int:
@left in probe: any
@left in grab: exec
var held: any = 0
var total: int = 0
on grab {
held = probe
if held.getType() is Type.Int {
total = total + held
}
}
@right out sum: int = total
Use a real type whenever you know it. The reference covers the rest in
any Type.
Modify
- Remove
as floatfromaverage. After a 3 and a 4 it shows 3: the divide is now between twoints and drops the remainder. - Count sixes. Add
var sixes: int = 0, add one whenlast == 6, and show the share of sixes as a whole percent: `(sixes.Value as float / rolls.Value- 100.0) as int`.
- Light a lamp while the average is above 3.5:
@right out lucky = average > 3.5.
Make
Make a fuel gauge. Each press of a button adds 7 liters to a 40-liter tank, but the tank never goes past 40. A sign shows “21 of 40 liters (52%)”, with the percent as a whole number, and a light is on while the tank is at least three quarters full. A second button empties it.
button1 Held -> chip fill
button2 Held -> chip empty
chip text -> decal Text
chip full -> light Enabled
Good to Know
round,floor,ceilandsqrtgive back afloat, even when the answer is whole. Addas intwhen you need anint.min,max,absandclampgive back the type they are given: anintstays anint.- Text that does not start with a number converts to 0, with no error.
Next: Lists.