Looping question?

I want to loop a question forever until the player chooses a particular answer, while locking the player into being unable to take other actions. Does anyone have any suggestions on how I can do this?

Set up a function first:






Enter this in the text input field: result

Then, add the first CASE:





Set the default up to call the loopMenu function again, since there is only one correct answer here:



  • I used the Split function to generate a list right then and there: Split("correct response;incorrect response;another wrong answer", ";")

  • I set up a default that calls the function again. (It also prints "Please try again", to avoid any confusion.)

Then, just call the function from wherever you'd like during any script.

Let's set it up on the start script, when play begins.


<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="looped menu">
    <start type="script">
  <object name="room">
    <inherit name="editor_room" />
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
  <function name="loopMenu"><![CDATA[
    ShowMenu ("What would you like to do?", Split("correct response;incorrect response;another wrong answer", ";"), false) {
      switch (result) {
        case ("correct response") {
          msg ("Yay!  You have answered wisely!!!")
        default {
          msg ("<br/>You said \"<b>" + result + "</b>\", which was incorrect.<br/><br/>Please try again.")



Alternate method using get input:


Awesome! This looks like exactly what I need. Thanks!

How could I go about passing in custom answers for each case?


You just click on Add (just to the right of Cases).

Make sure you wrap your cases in quotation marks, or they won't work!

The Split function is making your menu. Whatever is in your menu needs an identical case, with quotation marks around it.

I.e., if you want your menu to give the choices 'north', 'west', or 'south':

Split("north;west;south", ";")

(The ";" tells Quest to use the ; as a stop to separate the list sections.)

This would give the player this output:

What would you like to do?

1. north
2. west
3. south

You should have 3 cases (in this case):


You would also want to change this:

Show menu with caption What would you like to do?

to this:

Show menu with caption Which way would you like to go?



Oh, what I mean is, how do I pass in text for each case from outside of the function?

This is what I'm currently trying to do:


loopMenu (Split("one;two;three", ";"), "Response to one", "Response to two", "Response to three")

Try that in Code View, instead of just loopMenu.

...but you don't have quotation marks around your cases. It should be "one", not one.

The same goes for "two" and "three".

I don't think you need to return anything, either, but I haven't seen the rest of your code, so it's highly probable that my assumption is incorrect.

...and you need to put a question in that first text field, otherwise it will just print the choices. (You're probably just testing and know that already... Just pointing it out, just in case.)

Thanks! I think I'm a lot closer to what I need... However, when I select an answer, it's telling me the variable within the case script doesn't exist. "Error running script: Error compiling expression 'one': Unknown object or variable 'one'"

I can call 'one' outside of the switch case anywhere else in the function, but not inside. Am I doing something wrong, or can switches not grab variables outside of them? Or maybe I misunderstood something you said...

Here's my function:


And here's where I'm calling it:


Oh, I'm sorry!

I forgot that you can't pass local variables into ShowMenu. You'd have to use a global variable.

It can be done.

You'll have to either use a dictionary or set three attributes on the game.

Be right back with the easiest method. (Probably the attributes.)

All you need to change is the function:


But, you're wasting the loop function that way.

Add one extra thing to your list, so you'll have an incorrect answer to choose from. (The player can only select a choice here. You need to use get input instead of ShowMenu if you want the player to be able to enter anything in response to your question.)


Then do this (you'll have to alter the way you call loopMenu inside of loopMenu to avoid another error):



you can also use the 'while' Function too (though I've not done much testing with it, if there's any issues that come up):

// example scripting:

terminator_boolean_variable = false
while (not terminator_boolean_variable) {
  ask ("Stop?") {
    if (result) {
      terminator_boolean_variable = true

Thanks a bunch, guys! Looks like I have something functional to play with now. :)

That while script throws an error, HK. (I was excited about it when I saw it, though.)

I think it's because while and ask are both 'waiting' functions.

ah, I was afraid of that.

the 'while' Function in quest is only good for any type of scripting that does NOT need human input, as KV pointed out (KV tested), you get errors due to having multiple human inputs, when there can only be one at a time.

the 'while' Function does not wait for human input (using the 'ask, Ask, get input, show menu, ShowMenu, etc' Functions), and thus it runs again, before the human can give an input, and thus you got two input requests, when there can only be one at a time, and hence the error.

in normal programming, instead of doing tail-recursion, you should do a 'while / do-while' loop, as it's more efficient. (Anytime you got tail-recursion, you can replace it with a 'while / do-while' loop).

That doesn't work because ask is non-blocking. You can't put it in a while loop, because the program doesn't handle the user input until all other scripts have finished. The instruction would translate into English as "Make a note to ask the user this question, as soon as you're not busy dealing with scripts." And it doesn't matter how many times the game engine makes that note, because it never gets around to giving an answer.

Yeah, I suck at metaphors





(I can never remember that term!)

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