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:
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
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.
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:
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.
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.