Only allowing a single dice roll in a Twine passage

Hey all,

I have currently written a Twine passage (Sugarcube) which allows for a 2d6 dice roll and updates the player’s score accordingly.
Whatever changes I have tried, it’s currently possible for the player to roll the dice as many times as they like (thus giving them infinite opportunities to get the highest scores possible!)

Has anyone had any experience of writing dice-rolling macros that limits the player to just one dice roll? - My code contains variables such as:
<<if not $diceRolled>>
<<link “Roll the Dice”>> (etc)
And
<<set $diceRolled to true>>

Whatever I try the player can still roll multiple times

Thanks in advance

To prevent cheat by reloading, you need to separate the roll and the reporting, committing the result before printing it.

If you have something like:

<<set $roll to random(1,6) >>
You rolled $roll.

the player can cheat by reloading because the roll and the reporting are both re-evaluated.

But, if you commit the dice roll on the previous passage:

[[Roll->report_roll][$roll to random(1,6)]]

you can just print the result on report_roll, and it’ll be immutable:

You rolled $roll.

If your processing is more complex, have the computation on a passage that ends with:

<<goto "report_roll">>

and it’ll work.

2 Likes

Thanks n-n

1 Like

Just to be clear, the end-user can still alter such a dice roll by executing JavaScript like the following within their web-browser’s Console just before they select that link…

$(document).one(':passageinit', function (ev) {
	SugarCube.State.variables.roll = 6;
});

Or if they know that the variable name doesn’t change, and that the maximum value that variable will contain is consistent, then their could execute JavaScript code like the following in that Console at the start of a playthrough…

$(document).on(':passageinit', function (ev) {
	SugarCube.State.variables.roll = 6;
});

…and that variable will always equal that value after ever Passage Transition.

@TobiasRoadRunner
The simple fact is that you can’t stop the end-user from altering the outcomes, or as you put it “cheating”, as there are always ways around any efforts you take to do so.

And besides their own playthrough, who else are they affecting by doing such alterations?