Check a string variable for key words

Get help using Construct 2

Post » Sun Jan 19, 2014 2:04 pm

Hi, I am currently making an npc system and using string variables to input text.

So what I need is to also use the same string variable to call an avatar image while the npc is speaking. The string variable may look like this:

"(av:james) Hi my name is james"

Basically I need to tell construct to not print out the part of the string variable that defines the avatar(av:james). And at the same time use (av:james) as a condition to display the avatar of the character james.

Is it possible to make construct check a string variable for a keyword and if it's in the string variable, to remove it from the printed text.   
B
40
S
15
G
4
Posts: 426
Reputation: 5,870

Post » Sun Jan 19, 2014 2:14 pm

look at tokenat()
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Sun Jan 19, 2014 3:15 pm

Hi, can you elaborate a little bit more? I am still learning this
:)
I was thinking of using the find() expression but couldnt find any examples of that in action.
B
40
S
15
G
4
Posts: 426
Reputation: 5,870

Post » Sun Jan 19, 2014 4:00 pm

TokenAt() is a good way to do that. Then C2 manual say:

tokenat(src, index, separator)
Return the Nth token from src, splitting the string by separator. For example, tokenat("apples|oranges|bananas", 1, "|") returns oranges.

tokencount(src, separator)
Count how many tokens occur in src using separator. For example, tokencount("apples|oranges|bananas", "|") returns 3.

So, rather than "(av:james) Hi my name is james" you might just want to have "james|Hi my name is james"

then tokenat("james|Hi my name is james",0,"|") will return the avatar, and tokenat("james|Hi my name is james",1,"|") will return the text for him to say.

You can chain together strings, using different tokens if you want.

"james|Hi my name is james#susan|Hello there!#ben|Good day, eh?"

just be sure to use tokens that wont appear in your text, or if you are going to pass the strings to php (to save in a database) don't use a symbol that may cause trouble there (like "&")
B
99
S
35
G
20
Posts: 438
Reputation: 17,910

Post » Sun Jan 19, 2014 4:24 pm

I'm glad that @AllanR answered before me - I never knew about chaining them together.

Every day I learn something new - I just wish I could remember it all !
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Sun Jan 19, 2014 5:11 pm

[QUOTE=AllanR] TokenAt() is a good way to do that. Then C2 manual say:

tokenat(src, index, separator)
Return the Nth token from src, splitting the string by separator. For example, tokenat("apples|oranges|bananas", 1, "|") returns oranges.

tokencount(src, separator)
Count how many tokens occur in src using separator. For example, tokencount("apples|oranges|bananas", "|") returns 3.

So, rather than "(av:james) Hi my name is james" you might just want to have "james|Hi my name is james"

then tokenat("james|Hi my name is james",0,"|") will return the avatar, and tokenat("james|Hi my name is james",1,"|") will return the text for him to say.

You can chain together strings, using different tokens if you want.

"james|Hi my name is james#susan|Hello there!#ben|Good day, eh?"

just be sure to use tokens that wont appear in your text, or if you are going to pass the strings to php (to save in a database) don't use a symbol that may cause trouble there (like "&")[/QUOTE]

This is super awesome!! Much rep to you sir. :)
What a great solution. And thank you for taking the time to explain how tokens work
B
40
S
15
G
4
Posts: 426
Reputation: 5,870


Return to How do I....?

Who is online

Users browsing this forum: pegasus6464 and 29 guests