Check a string variable for key words

0 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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.   <img src="smileys/smiley1.gif" border="0" align="middle" />

  • look at tokenat()

  • 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.

  • 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 "&")

  • 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 !

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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 "&")

    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 <img src="smileys/smiley12.gif" border="0" align="middle" />

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)