So, I was writing a thing to allow players to enter a string of descriptive text, and have characters in game react to it intelligently. (Specifically I wanted players to be able to specify a color to dye their hair, and have characters comment/react.) In doing so, I kinda wrote a little subroutine that is basically the JavaScript string includes()
method, which is to say that it just checks a one string against another to see if the first string contains the second.
Anyways, this should plug and play into anyone’s subroutine list fairly easily. It only requires one variable to be created, specifically log
which will be set to true
if the string is included, and false
otherwise. There’s two required parameters (the string to check, and the string to check for), and two optional ones (a boolean case-sensative swtich, which defaults to false
, and a boolean whole word check, which should force the subroutine to only return true if the string matches as a whole word, which defaults to true
).
Or to put it into action, you can to check if “a quick fox jumped over the fence” contains the word “fox” with this:
*gosub includes "a quick fox jumped over the fence" "fox"
*if log
There was a fox there!
*if not(log)
I didn't notice a fox.
Or to be move involved, if you wanted to ask a player their hair color, the comment on it, you can do something like this:
*create hair_color
*input_text hair_color
*gosub includes hair_color "red"
*if log
Did you know that only about one or two percent of the population has red hair?
*gosub includes hair_color "rainbow"
*if log
Oh, you dyed your hair the colors of the rainbow? That's beautiful
...etc.
That the above code, even if a player enters “cherry red” or “copper red”, or “a deep, dark red like the the blood of my enemies”, it will make the comment about red hair.
Anyways anyways, I wanted to release this one because I’m only capable of testing so much on my own, and while I haven’t been able to find any bugs in it, I’m certain someone else will. If you do manage to find and error in it, please report it back to me so I can fix it.
Oh! And it dumps a bunch of text as a log if you run randomtest with “show full text” on. You know, in case you cared.
Code for anyone that wants to use it.
*label includes
*comment Required variable: log(false) [Required to retain information outside of subroutine scope]
*params s v
*comment param_1 s:(string) String to search.
*comment param_2 v:(string) Value to search for.
*if param_count >= 3
*temp c param_3
*else
*temp c false
*comment param_3 c:(boolean) Case-sensitive: Check is case sensitive (optional: defaults to false)
*if param_count >= 4
*temp w param_4
*else
*temp w true
*comment param_4 w:(boolean) Whole words: Check only searches for whole words (breaks on spaces) (optional: defaults to true)
*temp x 1
*temp y 1
*comment x:place in string searching from; y:place in value searching from
*if not(c)
*set s "$!!{s}"
*set v "$!!{v}"
*label includes_check_letter
*if choice_randomtest
RTLog: Checking character ${(x+(y-1))} of string (${(s#(x+(y-1)))}) against character ${y} of value (${v#y}):
*if (s#(x+(y-1))) = (v#y)
*if choice_randomtest
True
*set y + 1
*if y > length(v)
*if choice_randomtest
RTLog: Value matches part of string.
*if not(w)
*set log true
*return
*elseif w
*if choice_randomtest
RTLog: Checking if either current place to check in string (${(x+(y-1))}) is greater than string length (${length(s)}) or current place in string is a space:
*if (x+(y-1)) > length(s)
*if choice_randomtest
String Ends
*set log true
*return
*elseif (s#(x+(y-1))) = " "
*if choice_randomtest
String character (${(s#(x+(y-1)))}) is a space
*set log true
*return
*else
*if choice_randomtest
False [String character is (${(s#(x+(y-1)))})]
*goto includes_reset_check
*else
*bug
*goto includes_check_letter
*if choice_randomtest
False
*label includes_reset_check
*set y 1
*label includes_check_word
*set x + 1
*if x > length(s)
*set log false
*return
*if (choice_randomtest) and (w)
RTLog: Checking if character ${x} of string (${s#x}) is a space:
*if (w) and ((s#x) = " ")
*if choice_randomtest
True
*set x + 1
*if x > length(s)
*set log false
*return
*goto includes_check_letter
*elseif w
*if choice_randomtest
False
*goto includes_check_word
*else
*goto includes_check_letter