So the *rand stats screen bug is a classic at this point but for those unaware, this bug occurs when you use *rand on a page, enter the Stats screen and return. This forces the page you were on to reload and your previous *rand result will be overridden. This allows readers to re-roll random outcomes.
The general solution is to generate the random result on a previous page. This means that even if the Stats screen is entered, it won’t override / re-roll the random result.
My project uses *rand quite liberally and it was a pain to remember to add in *rand to the previous page, so I decided to pre-roll all possible outcomes as early as possible. It’s not a perfect solution but the implementation is easy to use and less error-prone. I thought someone else might also find it useful, so decided to share it here.
To use it, add the following file to your project.
subroutine_random.txt
====================================================
*label rand
*params min max
*temp random random_array[random_current_index]
*gosub normalise_random random min max
*comment random_array[${random_current_index}]=${random} -> ${random_result}
*comment *line_break
*set random_current_index +1
*return
*label populate_random_array
*temp max_loops random_array_count
*temp i 1
*label loop
*if (i <= max_loops)
*gosub generate_random
*set random_array[i] random_result
*set i +1
*goto loop
*return
*label generate_random
*temp r ""
*rand r 0 (random_precision - 1)
*set random_result r / random_precision
*return
*label normalise_random
*params random min max
*temp range (max - min) + 1
*temp raw_scaled random * range
*temp offset_scaled raw_scaled + min
*gosub floor offset_scaled
*return
*label floor
*params number
*temp floatingPoint (number modulo 1)
*temp integerPart (number - floatingPoint)
*set random_result integerPart
*return
Then add the following code to the top of your startup.txt:
*create_array random_array 1000 ""
*create random_current_index 1
*create random_precision 1000000
*create random_result ""
*gosub_scene subroutine_random populate_random_array
Now whenever you want a random number, you just call the subroutine:
*gosub_scene subroutine_random rand 1 100
You rolled a ${random_result}!
The benefit of this is that you don’t need to remember to add anything to the previous page. You call it exactly when you need it.
Some things to note:
- By default, the array stores
1000unique rolls. You can increase this if needed but be mindful of performance / memory issues. You only need to increase this if you’re planning to have >1000 random rolls over the course of a playthrough. Note: you will also need to update the loop limit. If you’re still having performance issues, you may want to have multiple smaller arrays (per scene) instead of one big global array. - By default, the randomness precision is set to
1000000. You can easily increase this if needed but be mindful of performance / memory issues. You only need to increase this if your random range will be larger than 1000000 and you need each number in the range to be distinct (this is extremely unlikely). - Any subroutines that return data will store it in a global variable called
random_result. If you’re familiar with other ChoiceScript libraries, it’s similar to how they usually do it too. The variable name can be changed fairly easily if you need to. - You can remove the comments from the subroutine
randmethod to view more details about the random roll that was generated.
You can view the live example (and code) here: CoGDemos
Anyways, I’m curious how other people deal with this issue so please do share your own solutions! I’m sure there’s more elegant waysout there but this does the job for me.
