Strings and Numbers in Choicescript

This is kind of random and probably old news, but I just discovered that strings containing numbers can essentially be used as numbers in choicescript. I was interested in this because I was storing a list of integers as a string (for an equipment system), and I wondered if I would have to create a str_to_int function. Thankfully that’s not necessary.

*temp a 123
*temp b "2"
*set a - b
a: ${a} ${a + b} ${b + a} ${a*b} ${b*a}

This prints a: 121 123 123 242 242 .

*set a "123"
*if (a = 123)
  a
*if (1 = "1")
  b

This prints a b, which means that the string "123" is equal to the number 123. Interestingly this is also what happens in Javascript. However the previous behavior is different from javascript: "121" + 2 would give "1212" in js.

“Numeric strings” can also be used as indices:

*temp array_1 1
*temp array_2 2
*temp array_3 3
*temp index "2"
${array[index]} 

This prints 2.

It goes the other way, too. Numbers can be concatenated to strings.

*temp d "abc_"&1
d: ${d}

This prints d: abc_1.

4 Likes

Thanks for sharing. I use this feature as a way to enter negative numbers.

*set var -1

will decrease the variable. To set it to -1, you can do

*set var "-1”

Another way would be

*set var (0 - 1)

4 Likes