Calling one digit of a variable [Sugarcube2]

Twine Version: Sugarcube2 (2.37.3)

Hello all, I’m trying to handle a problem I’m having with parsing variables. The Sugarcube2 documentation below seems to imply I can use brackets to read a single digit of a variable:

However when actually testing it with something like this:

<<set $tester to 124567111>>

$tester
$tester[0]
$tester[4]

I expected to see the full variable for “tester”, then the 1st digit, then the 5th digit. But it doesn’t work, instead I get this:

124567111
$tester[0]
$tester[4]

It seems the documentation was talking about arrays instead. Does anyone know how to quickly call a digit of a variable?

The $varname[index] notation is only for accessing Array elements, though there is a legacy Javascript feature that also allows it to work for characters of a string.

What it does not do, is access individual digits of a number, and I don’t think there is a way to do that without converting it to a string first. $varname.toString()[4] would work, for example, but you’ll have to put it inside a <<print>>.

e.g. <<print $tester.toString()[4]>> should give you “6”, as expected

Additional to @Hituro advice…

If you don’t intend to perform maths on the numerical value of that ($tester) variable, then this may be one case were using a String representation of a Number would be appropriate.

<<set $tester to "124567111">>

$tester
$tester[0]   => outputs 1
$tester[4]   => outputs 6

Interesting and possibly the fix for my use case (using 20 variables in place of 500 by using the digits). Is there a way to change a single digit in a string? I tried:

<<set $tester[4] to 8>>

But it tells me “Cannot assign to read only property ‘4’ of string ‘124567111’”

Actually I have an update after tinkering around and realizing that Hituro is right and at the end of the day, Twine is javascript with a hot makeover. So I looked for a javascript solution. It turns out you can keep it as a number and do this:

<<set $tester to 124567111>>

<<print $tester.toString().charAt(2)>>

And if you don’t want to print it and want to use it for if statements, you can do this:

<<if $tester.toString().charAt(2) eq 4>>
	Congratulations!
<</if>>

I take it from your reply that you are using this string of numbers as some sort of set of flags? To save on variables?

I’d point out that:

  1. There’s no real need to save on a small number of variables in this way
  2. You’d be much better advised to use a generic object to do this so that the flags can have sensible names

That is true, but I’m saving probably 1,000+ variables. I keep a lexicon of what number allocates to what event/dialogue in the past with a lot of characters. I feel like an extra 1,000 variables could certainly mess up my game’s loading.

My guess is that it would not. It’s actual size of data, rather than the number of elements, that matters. I’m sure it would have a measurable effect, but not a significant one.

Interesting, HiEV seemed to be in favor of bit flags

You will notice that the suggestion of using bit flags is last on that list of potential techniques, and that they aren’t using String conversion or Character manipulation to alter the value of a bit flag.