Searching a String for Specific Words
Shadecerule
07 Jan 2019, 02:45There is a point where I allow the player to type in a custom phrase, which I assign to a string variable (let's say Phrase). I want to be able to search this Phrase string to see if the player typed in any specific words.
For instance, let's say I'm looking for "sword", "wand", and "shield". I want to use a function to search the Phrase string for any of these terms. If the function finds any of the words I'm looking for, I want it to return true so that I can execute a unique event based on the matching string.
Additionally, how can I condense my if statements for this function? For instance:
if (Phrase [contains] "sword" or Phrase [contains] "rapier" or Phrase [contains] "stabby thing") {
(do sword-related thing)
}
Is there a way to condense all those or's into one thing?
hegemonkhan
07 Jan 2019, 04:44take a look at the 'String Functions' section/category:
http://docs.textadventures.co.uk/quest/functions/#string
and also in using list/dictionary VARIABLES:
http://docs.textadventures.co.uk/quest/functions/#list
http://docs.textadventures.co.uk/quest/functions/#dictionary
in particular:
'Instr' and/or 'InstrRev'
'Mid' for then getting that found string if need be
'replace' if you need to replace one string with another
'LengthOf' for getting the length of a string
'Split' and/or 'Join' for converting between strings and stringlists
'StartsWith' and/or 'EndsWith' can be useful as well, if you got/are-using a good naming/labeling system
using list/dictionary VARIABLES as alternatives for long 'if' Scripts (lots of 'else ifs', or, lots of use of 'and/or' operators)
'foreach' and 'for' with lists/dictionaries
'ListCountains' and/or 'DictionaryContains'
'ListCount'
I'll let you to try to figure out how to use these on your own, but if you need help, ask and I'll help you out with it
The Pixie
08 Jan 2019, 13:12It is a bit more advanced, but IsRegexMatch
would do it too. Also, use LCase on the text the player types to make it case-insensitive.
if (IsRegexMatch ("sword|rapier|stabby thing", LCase(Phrase"))) {
do stuff
}
Shadecerule
10 Jan 2019, 07:36Thanks! This should be perfect. I'll come back if I have anymore questions.