[Help] Using "If" to check contents of a String Value "Result"
Weezil
25 Oct 2016, 20:50My game allows the player to input a name. However, I want to be able to keep the player from using names that I deem as unavailable or off limits. My hope is that there is a way for me to use Get Input, and then after receiving said input, be able to use an "If" script to check the contents of the string for an instance of letters "wxyz" in that order, unbroken, and perform an action based on whether or not this combination of letters was discovered in the string.
Is this able to be done in the current program? Or will I need to find a workaround?
hegemonkhan
25 Oct 2016, 21:40if you mean checking for this substring of "wxyz" (with no characters/symbols between them) at the start, mid, or end, then you can use these:
http://docs.textadventures.co.uk/quest/functions/string/startswith.html
http://docs.textadventures.co.uk/quest/functions/string/mid.html
http://docs.textadventures.co.uk/quest/functions/string/endswith.html
^^^^^^^^^^^^^^^^^^^^^^^
http://docs.textadventures.co.uk/quest/functions/ (scroll down to the very bottom, to the 'String Functions' section/category, for String Manipulation Functions)
an example below:
<function name="name_function">
msg ("Name?")
get input {
if (StartsWith (result, "wxyz") or EndsWith (result, "wxyz")) { // using 'Mid' is a bit more complicated...
msg ("unacceptable input, try again")
wait {
ClearScreen
name_function
}
} else {
player.alias = result
}
}
</function>
otherwise, if you got characters/symbols between them... you'll need to use some somewhat fancy coding, using Stringlist Attributes so that you can do iteration
if you want to see a ton of examples of this type of stuff:
http://textadventures.co.uk/forum/samples/topic/4988/character-creation-crude-code-and-sample-game
as if you got any questions
The Pixie
25 Oct 2016, 21:53You could use a regex...
https://github.com/ThePix/quest/wiki/Pattern-Matching-with-Regular-Expressions
But the easy way is with Instr
, which will return zero if the substring is not found.
if (Instr(result, "wxyz") = 0) {
msg ("Not there")
}
else {
msg ("Found it!")
}
hegemonkhan
25 Oct 2016, 22:05I guess I should've looked at 'instr', whoops. Didn't realize it had such a 'substring' Function, didn't recognize 'instr' as it.