Quick question: 'contain' string

Get help using Construct 2

Post » Mon Mar 18, 2013 8:18 pm

Hello peoples.

A quick question. I'm experimenting with a 'terminal game'. I want the game to compare what's in a text input box, but how do I get it so that Construct checks to see if what's in the input box contains a word.

Here's an example:
I want the Construct to check if the input box contains the word "sit"
The user enters "sit up"

How do I get Construct to recognize that "sit up" has "sit" in it?

- Thanks
B
13
S
2
G
4
Posts: 54
Reputation: 3,882

Post » Mon Mar 18, 2013 8:54 pm

From the manual on system expressions:

find(src, text)
Find the first index within src that text occurs, else returns -1.

So, you can use find(textbox.text, "sit") and if it comes up with anything other than -1, the word sit is in there somewhere. Unfortunately, it would also find any words containing "sit", such as situation. If you wanted to make sure it was a specific word you could go with:

for "loop" 0 to tokencount(textbox.text, " ")
-Compare to values: tokenat(textbox.text,loopindex("loop")," ") = "sit"

That would break the string up by spaces (i.e. words, if the person isn't a bad typist like I am) and check each piece individually.[URL][/URL]
B
26
S
8
G
3
Posts: 210
Reputation: 5,973


Return to How do I....?

Who is online

Users browsing this forum: Mohicano and 34 guests