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:

image

image

image

image

image

Enter this in the text input field: result

Then, add the first CASE:

image

image

image

image


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

image

NOTES:

  • 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.

image


<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="looped menu">
    <gameid>56152bd4-5e73-4f11-b5c6-7559a4225f7b</gameid>
    <version>1.0</version>
    <firstpublished>2017</firstpublished>
    <start type="script">
      loopMenu
    </start>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
    </object>
  </object>
  <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.")
          loopMenu
        }
      }
    }
  ]]></function>
</asl>

Resources:

https://textadventures.co.uk/forum/quest/topic/6074/wait-while-a-loop-recursive-function-is-running#42008
http://docs.textadventures.co.uk/quest/multiple_choices___using_a_switch_script.html
https://textadventures.co.uk/forum/quest/topic/5303/looping-a-get-input-script
http://docs.textadventures.co.uk/quest/conversations.html

Alternate method using get input:

http://docs.textadventures.co.uk/quest/asking_a_question.html


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

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


image

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):

"north"
"west"
"south"


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?


http://docs.textadventures.co.uk/quest/multiple_choices___using_a_switch_script.html

http://docs.textadventures.co.uk/quest/functions/string/split.html


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:

https://i.imgur.com/BGpvWvn.png


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:

https://i.imgur.com/zPg2B9z.png

And here's where I'm calling it:

https://i.imgur.com/eyYBhyY.png


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:

image


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.)

image


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

image

image


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


non-blocking

non-blocking

non-blocking

non-blocking

(I can never remember that term!)


Log in to post a reply.

Support

Forums