Random numbers

I'm trying to develop a game based around using random numbers, probability odds etc...

Can anyone give me some help with how to do this?

For example, if I want Quest to generate a number between 1 and 10, and then print different things depending on which number is generated, then how do I do it?


This is from a "tutorial game" that I've been adding to for some time. In this "game" I have an object 'birds' and when you look at them, they appear to be flying in different directions. This was my explanation of it in-game:

Explain birds (you can apply this same logic to any script that you want. As noted, this is a script placed under the look at section for the birds, but it could be applied to a 'spin' verb on a wheel, or a n-sided dice roll (verb roll on dice), etc):

  1. Click Add new script under your object (in this case, the "birds').
  2. Find 'Set a variable or attribute'.
  3. To keep it simple, set the variable 'x' = random number between number 1 and number 4. I chose 4 in this case because I wanted to print a random message for each of the four directions I wanted the birds to fly (n,s,e,w).
  4. Now, add another new script. Add an 'If' script (can be clicked at top right of box.
  5. In the 'If' of this script, it should look like this --> If expression x = 1
  6. Then set variable direction = expression "north."
  7. I repeated this for my total number of possible outcomes only changing the x value to 2, 3, 4 and changing the expression equal to south, west, and east.
  8. At the bottom of this block of script, add another new script.
  9. Choose print a message. Change the message box after print to expression.
  10. I typed a message like this for my bird example WITH the quotes!: "You watch the birds and they fly off toward the " + direction.
  11. What this does, is it take the 'direction' variable you set earlier, and places in a random direction 1-4.

There might be an easier way which someone will come point out I'm sure, but this is how I know how to do it.


There are 3 built in functions for random numbers, those being RandomChance, GetRandomInt, and DiceRoll. For general purpose use I'd recommend GetRandomInt, and it basically does exactly what you described for picking a number.

For the part where you want the script to do something based on that number you can use either an if else chain or a switch. Personally for something like this with potentially a lot of outcomes I prefer a switch. They are functionally the same, but I find switches are easier to read and feel more organized to me when there are several cases.


Sorry to be annoying, but I'm completely new to Quest and have no programming experience...can someone try and explain this to me in a really easy way, step by step?


First make a command name it random then above the name type in the word roll, then in gui editor add new script find variables and select set variable or attribute. Then click the expression tab for a drop down menu and select random number, now set the name of the variable (may i suggest a letter like i). After that add new script print message and use drop down tab on message an choose expression then type this in ("The result is " +i+".") with out () brackets. After that you're set go into game and type in roll and hit enter and it will give you a random result each time. Here is code in case you want to just use code view and copy and paste ;)

i = GetRandomInt(1,10)
msg (" The result is " +i+".")

I tried entering that and it didn't work. The error message was:

Error running script: Expected 1 parameter(s) in script 'msg "( The result is " +i+".")'


possibly, you just typo'ed it (coding requires you to be grammer/spelling/typo nazi, you got to be 100% ERROR free!)

msg ("The result is " + i + ".")

two types of "chunks":

1. "TEXT"
2. + VARIABLE +

the "chunks" (3) are:

1. "The result is "
2. + i +
3. "."

msg ("The result is " + i + ".")

where, at least in your post, you've got wrongly:

msg " (The result is " + i + ".") // your first/left-most double quote needs to be within the parenthesis. This is the 'msg' Function and the content/stuff within the parenthesis is this Function's Parameters (which are inputs to be used by the Function's body/scripting/scripts)

if you've not gone through the entire tutorial, it's really helpful to do so first:

http://docs.textadventures.co.uk/quest/#Tutorial

then, it'd be helpful to learn the "bread and butter" of programming (and quest's programming): Attributes and the 'if' Script usage

http://textadventures.co.uk/forum/samples/topic/5559/attributes-and-if-script-guide-by-hk

because what you want to do, is an application of using Attributes and the 'if' Script, so you really need to try to understand this stuff first...


here's an example game for you to study:

(requires the offline/desktop/downloaded quest software/engine)

(create a new game -> save it -> quit/close the game -> right click on your newly created game file 'xxx.aslx' on your computer -> choose to open it -> open it with a text editor software: notepad, wordpad, apple: text editor, notepad++, or whatever -> highlight the entire game code -> delete all of it so your game file is now blank -> highlight this post's entire game code in the code box below -> copy it -> paste it into your game file -> save your game file -> open your game file in/with the GUI/Editor, now being able to see what it looks like as GUI/Editor scripting and etc, study it, and you can also play the game and see it in action)

(alternatively, another way to get to the entire game code, is within the GUI/Editor, at the top of screen's menu bar is a notepaper-like button between the 'play' and 'help buttons, this notepaper button is a toggle for going between the GUI/Editor view mode and the full Code View mode, so press it to go into the full Code View mode, which you can then delete all of the game code, and paste in my code below into it, saving it, and then studying it, back in the GUI/Editor view mode, and/or playing it to see it in action)

(for/when doing the individual 'add new scripts' in the GUI/Editor mode view, in its window/box there's also a notepaper-like button on its right side, this is to code-in what you want just for this individual added new script. This is NOT your entire game code)

your entire game code is the 'asl' tag block (so make sure you highlight and copy the 'asl' beginning and ending tags too):

<asl version="550"> // beginning 'asl' block tag
  // your huge mass of code/content of your game
</asl> // ending 'asl' block tag

here's my example game/game-code for you:

<asl version="550">

  <include ref="English.aslx" />
  <include ref="Core.aslx" />

  <game name="example_game">

    <attr name="gameid" type="string">f63f45df-3dba-4dfe-b427-6a76381afd2a</attr>
    <attr name="version" type="string">1.0</attr>
    <attr name="firstpublished" type="string">2017</attr>

  </game>

  <object name="room">

    <inherit name="editor_room" />

    <object name="player">

      <inherit name="editor_object" />
      <inherit name="editor_player" />

    </object>

  </object>

  <object name="npc_1_object">

    <inherit name="editor_object" />
    <inherit name="npc_type" />

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

    <attr name="alias" type="string">john</attr>

    <attr name="dialogue_stringdictionary_attribute" type="stringdictionary">

      <item>

        <key>1</key>
        <value>hi, my name is john</value>

      </item>

      <item>

        <key>2</key>
        <value>What is your wish?</value>

      </item>

      <item>

        <key>3</key>
        <value>Who are you?</value>

      </item>

    </attr>

  </object>

<object name="npc_2_object">

    <inherit name="editor_object" />
    <inherit name="npc_type" />

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

    <attr name="alias" type="string">jack</attr>

    <attr name="dialogue_stringdictionary_attribute" type="stringdictionary">

      <item>

        <key>1</key>
        <value>water is wet</value>

      </item>

      <item>

        <key>2</key>
        <value>the sun is bright</value>

      </item>

      <item>

        <key>3</key>
        <value>a circle is round</value>

      </item>

    </attr>

  </object>

  <type name="npc_type">

    <attr name="displayverbs" type="listextend">dialogue</attr>

    <attr name="dialogue" type="script">
      dialogue_function (this.dialogue_stringdictionary_attribute)
    </attr>

  </type>

  <verb>

    <property>dialogue</property>
    <pattern>dialogue</pattern>
    <defaultexpression>You can't have a dialogue with that!</defaultexpression>

  </verb>

  <function name="dialogue_function" parameters="stringdictionary_parameter">

    integer_variable = GetRandomInt (1, DictionaryCount (stringdictionary_parameter))
    string_variable = ToString (integer_variable)
    msg_string_variable = StringDictionaryItem (stringdictionary_parameter, string_variable)
    msg (msg_string_variable)

  </function>

</asl>

let me know if anyhting goes wrong (I probably made a mistake somewhere, lol) or if you need help with anything or need anything explained in more detail or better/more clearly.


I tried entering that and it didn't work. The error message was:

Error running script: Expected 1 parameter(s) in script 'msg "( The result is " +i+".")'

You typed it wrong. You have:

msg "( The result is " +i+".")

Should be:

msg (" The result is " +i+".")

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

Support

Forums