ChoicescriptIDE / main

32 stars 6 forks source link

Warn on use of rand before a page break #114

Open CareyJWilliams opened 3 years ago

CareyJWilliams commented 3 years ago

The way that the stats menu is implemented in ChoiceScript means that the main scene code is actually re-executed when you leave the stats screen. In most circumstances code will be deterministic and re-execute as expected. However, if you're using the rand command, then things can get tricky.

Take the following example:

*create player_cash 10
*temp rand_var 0
*rand rand_var 1 100
You won ${rand_var} gold!
*set player_cash + rand_var
You now have ${player_cash} gold.
*page_break

Here we generate a random amount of gold, display the value to the player, and add it to their total gold. Simple enough. Now, what if the player decides to visit the stats screen at this point? The code will re-execute when they leave it, and you guessed it, we might end up with a different random value (and thus total gold value).

Needless to say this can open the game up to abuse. The solution is to "roll" the random value a page (or more) before you need it. I think it should be possible for us to issue a validation warning on any usage of a *rand(ed) variable that occurs before a page_break(ing) command (page_break, choice, finish), which should help authors avoid this sort of error more often.