Searching a string for a word?

For questions about using Classic.

Post » Tue Mar 18, 2008 4:37 pm

Is it possible to search a string for a word? I'm thinking of creating a complex AI, and i want to easily be able to add different skills to the AI. So for example, if i want him to be able to see in the dark, i would simply open his "atributes" string and write "NightVision". Then the code applies certain events for every enemy with nightvision! I could use a bunch of private values, and have for example the value "NightVision" set to 1 if it should apply to that enemy, but it would require more repetition, and be harder to organize. Searching for a word could be used in other ways too, so if it's not allready in construct i suggest you ad it to the to-do list!
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

Post » Tue Mar 18, 2008 5:41 pm

This should be possible to do in Python.
Something like this maybe
[code:1c1wabwq]
if my_attribs.find("NightVision"):
# Yay, I'm a cat!
[/code:1c1wabwq]
B
2
S
1
G
5
Posts: 50
Reputation: 1,500

Post » Tue Mar 18, 2008 7:02 pm

You can do this with the Compare Values condition and the Find() expression:

Find("Source string, with different words", "different")
different to
0

would mean the word "different" existed in the source string.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Wed Mar 19, 2008 1:02 am

Perfect. Thank you guys!
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

Post » Tue Apr 22, 2008 8:09 pm

I'm bumping this thread now... so what? i don't care! :twisted:


eeh.. Is it possible to search for many words at the same time? Something like

Find("Source string, with different words", "hello", "dude!")
different to
0

Exactly what i wrote seems to do something, but i can't figure out what it does.
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

Post » Thu Apr 24, 2008 11:42 am

Surely you could make that another condition in the event? Provided you wanted it to only run the event if BOTH words could be found.
B
2
S
2
G
5
Posts: 448
Reputation: 2,546

Post » Thu Apr 24, 2008 11:46 am

The third parameter to Find is the index at which to start searching, but the expression validator accepts a string (eg. "55") but "dude!" will be converted to 0 :P so it doesn't do what you think... a bit dubious design on my part perhaps. Use a function or condition alias to search for several words at once, maybe.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Thu Apr 24, 2008 3:23 pm

Nice! I'll use the function now that you've told me how it worked. I used multiple conditions before, but one event was bigger then the screen, so it got kind of annoying. the function take up less space.
B
8
S
2
G
5
Posts: 744
Reputation: 3,288


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest