New And Can't Code: Adding DiceRoll Function

Hello! I am new to this and completely terrible.

I am trying to create a gamebook that uses the DiceRoll Function. (http://docs.textadventures.co.uk/quest/functions/corelibrary/diceroll.html)

I'm just completely unsure how to use the function/script on the page. I'm so sorry. I stared at the documentation and tutorials, but I'm so confused.

My goal is to have a 1-20 random number roll and the resulting integer would lead to different messages and page-links using If and Else.

I set the Page Type to: Script+Text

I'm given the helpful option then to add a script command. ... And this is where I get lost.

I've tried adding a 'call function DiceRoll', but I'm honestly just not sure how to make it work.

I also tried emulating the DiceRoll Function by adding a function of my own, but I wasn't sure what the 'return type' should be, what the parameters were and how to write the script other than copying what was printed on the DiceRoll page.

I'm very sorry, since I imagine the answer to this is extremely simple to most people. Thank you so much in advance.


There's a few different ways to do this, but they all involve quite a bit of code. So I'll post the actual code here. If you want to know how to input it using the graphical script editor, it will be easier to click on "code view" and paste the code in. Then you can go back to the GUI view to see what it looks like there.

You probably don't need to use DiceRoll. It's more efficient to use GetRandomInt, if you're only rolling a single die.

You need the result of the function to be stored in a variable, so your script would look something like:

diceroll = GetRandomInt (1, 20)
if (diceroll < 4) {
  // script goes here for if the dice roll is less than 4
}
else if (diceroll < 9) {
  // The script that is here will be run if the roll is 4-8
}
else if (diceroll < 16) {
  // another piece of script goes here
}
else {
  // A script for if none of the others run
  // so 16+ in this case
}

GetRandomInt gets a random number between the two numbers you specify. It's only worth using DiceRoll if you're rolling multiple dice and adding them together; or if the dice you roll aren't always the same.

The scripts for each option can display messages, change the page's options, or send the player to a different page; whichever you prefer. It's worth noting that for a "Script + Text" page, if the script sends the player to another page then the text will not be displayed.

If you have many options in a script like that, you may find that switch/case is better than if. It would be something like:

switch (GetRandomInt (1, 20)) {
  case (1,2,3) {
    // script goes here for if the dice roll is less than 4
  }
  case (4,5,6,7,8) {
    // The script that is here will be run if the roll is 4-8
  }
  case (9,10,11,12,13,14,15) {
    // another piece of script goes here
  }
  default {
    // A script for if none of the others run
    // so 16+ in this case
  }
}

Wow! Thank you! You've given me an amazing gift. Very easy to understand and extremely helpful. Thank you so so so much for taking the time to answer!!!


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums