Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Is there a conditional operator for "contains" ?

I'm using Twine 2, Sugarcube.

I've set up a text input and have it so if the user inputs certain words, specific phrases will appear. However, this obviously only works if the user inputs those exact words, using the conditional operator "eq" like so: << if $input eq "word" >>

Is there an operator that is equivalent to "contains" or something similar? So that if the user inputed the sentence "I like cute kittens" I could make something happen just because the sentence contains the word "kittens" in it?

Thanks so much for your help!

-WR

Comments

  • You don't state if you are using version 1 or 2 of SugarCube, so I will assume its 1.

    You can use a combination of two function, Javascript's split and SugarCube's contains to do what you want.

    Try the following:
    /% In your code $string would contain the value from a text input field. %/
    <<set $string to "I like cute kittens">>
    
    /% Should test that the Reader actual entered a value. %/
    <<if $string neq "">>
    	<<if $string.split(" ").contains("kittens")>>
    		There are kittens
    	<<else>>
    		No Kittens found!
    	<</if>>
    <</if>>
    
    ... I formatted the above to make it more readable, you can safely remove the extra indents and line-breaks.
  • Yes, it was SugarCube 1 -- and that worked beautifully, thank you!
Sign In or Register to comment.