custom commands integers all

How can I take a custom command for an integer and compare it with a variable attribute? I don't need this script to override all other possible commands but I do kind of want it to do nothing if anything but an integer is ever typed into it, or else give an inaccuracy response, not something else.


Not sure if I understand the question here. Do you mean you want a command where the player types a number?

A regex pattern like ^(?<text>\d+)$ should work; although you would have to use ToInt(text) to get its value.


Yes, I know how to do some things with Quest, just from experience, but I have not got any other script or code experience, just Quest, and I have not ever used a command to get an integer.

So, if I place ^(?<text>\d+)$ into the command pattern blank, the script I use for "Get Result" isToInt(text)?

I was not sure how to put that into the game, so I am reading
https://docs.textadventures.co.uk/quest/pattern_matching.html#The%20Regex
BUT I AM NOT MAKING ANY SENSE OF IT, except that regex = (maybe) ^(?<text>\d+)$belongs in the script, somewhere.


There are two ways to specify the pattern for a command. "Command patterns" look like get #object#, while regular expressions look like ^get (?<object>.+)$ (those are two different ways to write the same command). Regular expressions are generally more powerful; but harder to understand if you're not used to them.

The parts used for the example above are:

  • ^ matches the beginning of what the player typed
  • $ matches the end of what the player typed
  • (?<variablename>pattern) puts the player-typed text that matches pattern into the variable variablename
  • \d matches any digit
  • + means the previous thing in the pattern matches 1 or more times.

So… \d+ matches any set of one or more digits.
(?<text>\d+) takes a set of one or more digits from the player's command input, and puts those digits into the variable text.

So you could have a command whose pattern looks like ^enter code (?<text>\d+)$, and then the player could enter the command enter code 1234; and within the command script, text would be the string "1234".

the script I use for "Get Result"

Not sure what you mean here. Are you using a script to ask the user for input? I assumed initially that you wanted the player to type an integer as part of a command; in which case you would put it in the pattern.


I want to get the answer to a random number problem where two numbers are multiplied or added by the game and, when the command bar appears, the player types in a number, and the number typed in is either = or not = to the correct answer, a variable like the first and second numbers would be. like player. Answer
So...
if (player. Answer = ToInt(text)) {
}
That would work?
I guess I understand that.
Let me try it.


Assuming that player.Answer is the correct answer, and is an int, I would expect that to work.


I put ^(?<text>\d+)$ into the command pattern blank
and put into the command script
if (ToInt(text) = player.answer) {
msg ("Yes, correct.")
}
and I gave player. answer an integer of 21
I ran the game, typed in 21
and got
I do not understand your command.


I changed the command pattern to #text# and it works. While I think it would be great to know how to make the regex command pattern work, because that would leave room for other commands, and really you could also just regulate the command #text# to only work for integer answers in one room and not others, or at some points in the game, and not others, the regex would still be easier to use if it works, but I haven't been able to make it work, yet, and I do want no other commands to be used in the game, so I just should have tried #text#, but I didn't know how to use ToInt until I got that response from you, so, thanks, mrangel.
I know I had copy pasted it before. I suppose I should be less lazy, and try to see how everything works ahead of time.


There should be a drop down next to the pattern where you can choose "Command pattern" or "Regular expression", to tell Quest what type of pattern you're typing in.

In the case of #text#, that pattern will match any characters. So if the pattern is just #text# without any words, the command will run whether the player typed a number or a word. So it might be better to have something like:

if (not IsInt (text)) {
  msg ("You need to type a number.")
}
else if (ToInt(text) = player.answer) {
  msg ("Yes, correct.")
}
else {
  msg ("No, that's not correct.")
}

Although I do wonder if there's a way to make it match words too… could the response from ToWords be turned into a regular expression, so that your hypothetical player could type "twenty-one"? That's probably possible, but I suspect not worth the effort unless someone made a bunch of reusable functions for it.


If someone made the Quest text adventure engine able to recognize the words for every number in existence in every language Quest runs in, already, that would be amazing! I think a lot of people would enjoy that.
Either way, I don't mind a stubborn AI that says no, the answer is 311 if someone types in three-hundred-and-eleven.
But I learn a little more every day.


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

Support

Forums