How to randomize script activation?

Ok im really new to Quest and I dont have time to post a screenshot, im in a rush, and im sorry if this is really easy to solve I just suck at any sort of coding. I'm trying to make an editable chance of separate scripts activating after the player uses a verb, e.g the player attacks a slime and that slime has a chance of either dodging the attack or getting killed, i have been struggling with it for a while and i really can't solve it, if anyone could help that would be wonderful


here's the built-in Randomization Functions:

  1. DiceRoll

https://docs.textadventures.co.uk/quest/functions/corelibrary/diceroll.html

DiceRoll ("NUMBER_OF_DICEdNUMBER_OF_SIDES")

example:
integer_variable = DiceRoll ("1d6")

  1. GetRandomDouble

https://docs.textadventures.co.uk/quest/functions/getrandomdouble.html

example:
double_variable = GetRandomDouble ()

  1. GetRandomInt

https://docs.textadventures.co.uk/quest/functions/getrandomint.html

GetRandomInt (MIN,MAX)

integer_variable = GetRandomInt (0,100)

  1. RandomChance

https://docs.textadventures.co.uk/quest/functions/corelibrary/randomchance.html

examples:

boolean_variable = RandomChance (50)

integer_variable = GetRandomInt (0,100)
boolean_variable = RandomChance (integer_variable)


if you just got/want two choices/options, then you can just use:

RandomChance (50)

<object name="room">

  <inherit name="editor_room" />

</object>

<object name="slime">

  <attr name="parent" type="object">room</attr>

  <attr name="dead" type="boolean">false</attr>

  <displayverbs type="stringlist">

    <value>attack</value>

  </displayverbs>

  <attr name="attack" type="script">

    if (slime.dead) {

      msg ("The slime is already dead, silly")

    } else {

      msg ("You attack the slime")

      if (RandomChance (50)) {

        slime.dead = true

        msg ("Your attack hits the slime, killing it!")

      } else {

        msg ("The slime dodges your attack")

      }

    }

  </attr>

</object>

<verb>

  <property>attack</property>
  <pattern>attack</pattern>

  <defaultexpression>You can't attack that!</defaultexpression>

</verb>

if you want to do more than two choices/options, let me know, and I'll provide an example


if you need help with how to do the stuff in the GUI/Editor, let me know


You can use DiceRoll, but I'd recommend using GetRandomInt which is more efficient. For example, GetRandomInt (1, 7) will give you a random number between 1 and 7.

If you're choosing from multiple options and you want some to be more likely than others, it's good practice to get used to using a switch block. Like this:

switch (GetRandomInt (1, 6)) {
  case (1, 2, 3) {
    msg ("The code here will happen half the time")
  }
  case (4, 5) {
    msg ("This block has a ⅓ chance of happening")
  }
  case (6) {
    msg ("And this last option has a 1 in 6 chance of coming up")
  }
  default {
    msg ("This can't happen. But it's good to include an error message, so that if you make a typo it will be easier to find it")
  }
}

Thanks a ton for the responses, i figured it out with the help : ).


mrangel already covered how to handle more than 2 choices/options for (lazy) me, hehe :D

alternatively, if you got lots/tons of options/choices that you want to be more/less likely to happen, then it's better to use 'GetRandomInt' and 'RandomChance' together

also, instead of using the 'switch' Script/Function, you can also use the 'if' Script/Function too, as to which is better ('if' vs 'switch'), it depends... (they're practically functionally the same however, so again, it, "just depends", as to which one to use)

and there's also the use of 'for' and 'foreach' as well, which can be very helpful too

along with using List/Dictionary Attributes

but all of this is getting into more complex/advanced/fancy programming designs


(filler for getting my edited post, updated/posted)


Thanks a lot for the response, I copied your command replacing stuff like slime etc. to the stuff i need but it said: Failed to load game.
The following errors occurred:
Error: Error adding script attribute 'attack' to element 'skeleton': Invalid variable name ''
I don't know exactly what invalid variable name means but the name "skeleton" is a completely normal undercase word, no special symbols no spaces nothing. Sorry if i did something dumb, it would be great if you could provide the solution.

hmm, we'd have to see your code (easiest) (or you'd have to tell us exactly what you did, what words/names/labels you used, and etc), as you likely just messed up something, as you're trying to work with code, when not knowing how to yet

for using the GUI/Editor:

create/add that 'skeleton' Object to where-ever you want in the game

then click on the 'skeleton' Object in the left side's "tree of stuff", so it is highlighted, and then on the right side, click on the 'Verbs' Tab, and then add in the 'attack' Verb (with-OUT the single quotation marks)

and then for its scripting (add new scripts), you want it to look like this (in code):

(if you need me to help you doing these scripts via the GUI/Editor, I can, but it'll take a bit of work/time... I'm lazy, lol, so hopefully you can figure it out on own, but if not, then let me know, and I'll help you with it)

(or, if you can find and click on the 'code view' button (notepaper-looking button) in the popup window of the 'attack' Verb of the 'skeleton' Object, then you can just directly copy and paste my code below into it --- BUT you STILL HAVE TO ADD/CREATE the 'dead' Boolean Attribute on/within the 'skeleton' Object too, see bottom of my post on how to do that)

if (skeleton.dead) {

  msg ("The skeleton is already dead, silly")

} else {

  msg ("You attack the skeleton")

  if (RandomChance (50)) {

    skeleton.dead = true

    msg ("Your attack hits the skeleton, killing it!")

  } else {

    msg ("The skeleton dodges your attack")

  }

}

and also, if you're using my scripts, then you'd also have to add/create the 'dead' Boolean Attribute to your 'skeleton' Object:

'skeleton' Object -> 'Attributes' Tab -> Attributes (the box at the bottom) -> Add -> (see below)

(Object Name: skeleton)
Attribute Name: dead
Attribute Type: boolean
Attribute Value: false


I've been assuming you're using the Text Adventure version/mode... if you're using the Game Book version/mode, then let me know that! As a Text Adventure and Game Book are different


Log in to post a reply.

Support

Forums