Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Getting markers from dialogue line (String parsing)

Discussion in 'Scripting' started by X1r15, Jul 10, 2020.

  1. X1r15

    X1r15

    Joined:
    Dec 19, 2019
    Posts:
    12
    Hey,
    I am creating dialogue system for my game and I am wondering how should I approach one thing.

    I would like to create parser which changes dialogue parameters based on the markers in the string e.g.

    "Hey! [w1] You changed since the last time we have seen each other. [e2] [s2] I mean... [w1] In a positive way!"

    where:
    [w1] means Wait 1.0f,
    [e2] means Display expression 2

    I would like to create a Dictionary<int, Marker> where "int" is a indexOf marker and I am wondering what would be the most optimal way to do it.

    The index should point the the place in message without markers (that means after parsing the dialogue line should be "Hey! You changed since the last time we have seen each other. I mean... In a positive way!".

    My idea is to (in pseudo code):
    Code (CSharp):
    1. for character in fullMessage:
    2.     find index of "[" in fullMessage
    3.     find index of enclosing "]" in fullMessage
    4.     put in dictionary found above index of "[" and marker
    5.     remove marker (the whole "[w1]") from the fullMessage
    6.     adjust loop (e.g. iteration index) to accomodate string length change
    I am sure this will work but it just does not seem to be the "cleanest" solution. Any suggestions how differently I could approach the parsing?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    One suggestion is to look into TextMeshPro. TextMeshPro already has a tagging system built in for it's own tags, but what is nice is you can add your own tags into it. If you do that, TMP will hide these tags from the rendered text even though they still exist in the actual string.

    Just a thought.
     
    X1r15 likes this.
  3. X1r15

    X1r15

    Joined:
    Dec 19, 2019
    Posts:
    12
    In that case I definitely need to check TextMeshPro :). Thank you!
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    No problem. I used the same thing for a client project and it worked really well. TMP is pretty clean and flexible and you can do some extra stuff with it that the normal Unity Text can't do. Hope it works out for you.