How to prevent the player from inputting a specific set of strings after asked a question?

So, say I am asking a player their occupation, but I don't want them to input certain occupations, like "lawyer", for example. If they input "lawyer" after the "get input", they'll be asked the question again and ask for their input, but if they choose another one I don't want them to, then they will be asked again. I want it to loop until they choose an occupation that is available. My code currently looks like this:

msg ("")
get input {
  set (player, "occupation", result)
  if ((IsRegexMatch  ("\\b(lawyer|gardener|doctor|coordinator|explorer)\\b", LCase (result)))) {
    msg ("Originallity is a virtue, try again")
    undo
  }
  else {
    msg ("So your occupation is " + player.occupation + ", what an interesting job you have. Now that we know what you do, what's your name? (only first name)")
    msg ("")

However, when it comes to the "undo" part, it says there is nothing to undo and the thing breaks and the next questions can't be asked. Would using a list help?

If possible please answer in the normal view because I'm not too trained in code view yet. Could you help me resolve this? Thank you very much!


How is this part of code called? Is it part of a command?


The most common way to do this would be to put the question in a function. Then if the response isn't acceptable, you can just call the function again.

If you're going to be asking a lot of questions and you don't want to have a lot of functions, you could just use a script attribute to store the script, so you can use it again. For example:

game.question_response_script => {
  set (player, "occupation", result)
  if ((IsRegexMatch  ("\\b(lawyer|gardener|doctor|coordinator|explorer)\\b", LCase (result)))) {
    msg ("Originallity is a virtue, try again")
    msg ("")
    get input {
      do (game, "question_response_script", QuickParams ("result", result))
    }
  }
  else {
    msg ("So your occupation is " + player.occupation + ", what an interesting job you have. Now that we know what you do, what's your name? (only first name)")
    msg ("")
  }
}
msg ("")
get input {
  do (game, "question_response_script", QuickParams ("result", result))
}

or

game.ask_occupation => {
  msg ("")
  get input {
    set (player, "occupation", result)
    if ((IsRegexMatch  ("\\b(lawyer|gardener|doctor|coordinator|explorer)\\b", LCase (result)))) {
      msg ("Originallity is a virtue, try again")
      do (game, "ask_occupation")
    }
    else {
      msg ("So your occupation is " + player.occupation + ", what an interesting job you have. Now that we know what you do, what's your name? (only first name)")
      msg ("")
    }
  }
}
do (game, "ask_occupation")

If you're asking a lot of questions, it might end up neater to make a scriptdictionary, with the keys as the questions and each script handling one response. So you could have a function which asks the first question in the dictionary, and then calls itself again (either repeating the same question, or moving onto the next one after it gets a right answer).


Log in to post a reply.

Support

Forums