Extracting a letter from a string variable, then making it a condition

So on the ChoiceScript wiki, it talks about how we can extract a single letter from a variable by using #. For example:

*set firstname "Bob"
My name is ${firstname}. That's ${firstname#1}, ${firstname#2}, then ${firstname#3}...

#1 standing for the first letter in the string, then #2 for the second letter, etc. etc. This would look like this to the player:

My first name is Bob. That's b, o, then b...

If “firstname” was “Alice,” then the text would look different, of course, which is a nifty feature!

My question is: is it possible to then use that extracted letter in a condition?

For example, say I’m letting the player *input_text an adjective or color. “I want a _____ dress,” and they get to choose a color or style. But what if the word they input starts with a vowel? Then subsequently, the text would have to say “she wanted an orange dress,” rather than “she wanted a red dress.”

I figure if I can extract the first letter of whatever the player inputted, I could get around this with:

*if ({dresscolor#1} = "o") or ({dresscolor#1} = "a")
     *set dressa "an"
*else
     *set dressa "a"
You are wearing {dressa} {dresscolor} dress. 

Which would appear as “You are wearing a red dress” or “You are wearing an orange dress”.

Of course, this doesn’t actually work due to the curly braces. But is there a way to use that extracted first character of a variable for purposes other than just displaying it? I hope what I’m saying makes sense! :sweat_smile:

6 Likes

Your way works, I think. You just need to add the dollar sign and quotes. I tried it in my code and

*set a_name "Alya"
*if ("${a_name#2}" = "j")
    hi.

doesn’t display anything, but

*set a_name "Ajax"
*if ("${a_name#2}" = "j")
    hi.

displays “hi.” Should work for conditions too, I’d think.

5 Likes

:open_mouth: Oh, thank you! That works haha, I was missing the quotes and the dollar sign, which seems so simple in hindsight!

2 Likes

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the @moderators.