Dice rolling in a Gamebook


When making a random chance event in a Gamebook, is there a way how to show the player what number was "rolled"?

I want to make the clickable link to say i.e. "Pry the lock open (roll 1xd6 - 50% chance)" or "Persuade the barmaid (60%)" and once clicked, I would like the next page to start with the value rolled, so the player sees what happened - as when you roll a die during table top RPG.

I.e "You have rolled 3. You broke the lock and now nobody will be able to open it." or "Persuasion failed (42). The barmaid laughs at your proposal and walks off."

It would be easy to fake it with random command to be shown at the start of the page for d6 - You rolled {random:1:2:3} for fail page and You rolled {random:4:5:6} for success page. But to do this for d100 that would be quite a chore.

It would be normal to use a script for that.

You can use the function GetRandomInt() to roll a die, and put that number in an attribute. Then compare that number to a target, and use the result to decide which page to send the player to.

It looks like your existing system is using 2 random numbers; one to determine which page the player goes to, and then choosing a separate dice roll afterwards. This is pretty weird, but if you really want to do it that way, you could. Just use {= in the text processor to evaluate a Quest expression. For a 60% example, you could have You rolled {=GetRandomInt(1,60)} on the success page, and You rolled {=GetRandomInt(61,100)} on the failure page.

Thank you very much mrangel!

I have used 2 scripts indeed - one for real RNG chance, the other one to fake the result, as I had no idea you can actually make the editor to do the roll and show the number rolled too. Thanks a lot for the help!

I am still using the wysiwyg editor and am not well versed in scripts. This is very helpful and now I know what should I look into in more detail.

I had no idea you can actually make the editor to do the roll and show the number rolled too. Thanks a lot for the help!

There's 2 main ways to do it, with their own advantages and disadvantages. A lot of gamebook scripting stuff ends up being the same basic structure, so it's probably worth knowing how to do them.

The first would be having an option take you to a 'script' page which does the calculation. This makes the code a little simpler, but gives the player one more page to click through.
(For the examples, I'm assuming that "Page9" is the diceroll page, you need 4+ on 1d6 to succeed the task, "Page10" is the destination if you succeed, and "Page11" is there for failure)

You'd have something like:

this.diceroll = GetRandomInt (1,6)
msg ("{i:You rolled a {Page9.diceroll}.")
if (this.diceroll >= 4) {
  msg ("You push hard, and the door gives way.")
  msg ("{page:Page10:Continue}")
else {
  msg ("You push hard, but the door is stuck fast.")
  msg ("{page:Page11:Continue}")

Rather than displaying a message, you could just send the player straight to another page, and have the success/failure messages on pages 10 and 11. Like this:

this.diceroll = GetRandomInt (1,6)
if (this.diceroll >= 4) {
  MovePlayer (Page10)
else {
  MovePlayer (Page11)

(You can then use {Page9.diceroll} in the text on those pages to get the dice roll. The word this that I used in the script is a shortcut for "the current page", but it only works in script sections, not inside {} sections in the text. I have no idea why not, because in gamebook mode it would literally be one line added to the core code)

Note that if you use this method, checking for whether Page9 has been seen will always be false, because the player didn't end their turn there.

The other main method is to put the dice roll in the page that contains the link. This means there's no extra page, but the code is slightly more complex. You basically have a link that changes its destination based on some random choice. You'd make the page "Script and text", and have script like:

this.door_roll = GetRandomInt (1,6)
if (this.door_roll >= 4) {
  RemovePageLink (this, Page11)
  AddPageLink (this, Page10, "Force the door (roll 1d6, needs 4+)")
else {
  RemovePageLink (this, Page10)
  AddPageLink (this, Page11, "Force the door (roll 1d6, needs 4+)")

This adds one of two options based on a dice roll, but the player can't see which one has been chosen. This is a useful technique to know, because it's quite common for gamebooks to have some options that are only available based on where you've been before, or similar.

You might not need the RemovePageLink lines; I put them there so that it will still work if the player returns to this page a second time (so it removes the other option in case you got a different roll last time). I also named the attribute door_diceroll this time; so if you've got more than one choice that requires a dice roll, you can roll them all in advance and store them in different attributes. Then you can access whichever one you need from the results pages.

(Sorry for all the code; if you want to see what it looks like in the GUI, you can just paste it into the code view and then switch back; it's just that code is the easy way to share it on the forums)

Thank you very much! I will definitely play around it. :)

Log in to post a reply.