- Home /
Ignoring/not displaying text/numbers in string if surrounded with (), [] or <>?
Hello, I've got a dialogue system and I'd like to be able to set the speed at which the letters appear but change this per line of dialogue, my idea was to add numbers in the dialogue itself (IE, "Hello there young adventurer! [0.05]) and then having unity read the numbers within the brackets but not display the brackets nor the numbers on screen?
Is this possible? I've tried experimenting and googling but I've been unable to find anything that helps...
You could simply read from a JSON file.
{ "dialog1": { "text": "Hello there young adventurer", "speed": 0.05 } }
You can easily read this with any JSON Parser like $$anonymous$$iniJSON
As @ranch000 mentioned, JSON is the best option for you. But if you really don't want to use it, you should look into regex. It's not easy to get into, but it's the best tool for the job (outside JSON).
Only other option is to parse it manually. You would use methods like Substring to get the string from first index of '[' to first index of ']' and then parse that with double.Parse. This is very error-prone method, but it exists
Answer by Legend_Bacon · Oct 29, 2018 at 11:15 AM
Hello there,
As mentioned above, JSON might be an option. However, depending on the amount of tags you want your text to have (bold, italic, display speed, font size, font color etc...) the JSON might get a little big.
Instead, you can try this:
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
public class StringTest : MonoBehaviour
{
public string myInput = "This is a test[>s:0.5s<][>fsize:14fsize<]";
//Define you start and end tags here
public string speedTagStart = "[>s:";
public string speedTagEnd = "s<]";
public string fontSizeTagStart = "[>fsize:";
public string fontSizeTagEnd = "fsize<]";
private void Update()
{
Debug.Log(ApplyDialogueLineTags(myInput));
}
public string ApplyDialogueLineTags(string dialogueLine)
{
//your default values
float speed = 1.0f;
int fontSize = 12;
if (myInput.Contains(speedTagStart))
{
//You might want to add some security (try-catch?) here to make sure the value is correct
speed = float.Parse(ExtractTextInBetween(speedTagStart, speedTagEnd, dialogueLine));
}
if (myInput.Contains(fontSizeTagStart))
{
//You might want to add some security (try-catch?) here to make sure the value is correct
fontSize = int.Parse(ExtractTextInBetween(fontSizeTagStart, fontSizeTagEnd, dialogueLine));
}
//Do what you will with the values
Debug.Log("speed:" + speed.ToString());
Debug.Log("font size:" + fontSize.ToString());
//Remove all the tags from the string and return it
return Regex.Replace(dialogueLine, "\\[>.*?<\\]", string.Empty);
}
public string ExtractTextInBetween(string tagStart, string tagEnd, string input)
{
return input.Substring((input.IndexOf(tagStart) + tagStart.Length), (input.IndexOf(tagEnd) - input.IndexOf(tagStart) - tagStart.Length));
}
}
The code above outputs:
• speed: 0.5
• font size: 14
• This is a test
This code above allows you to have as many tags as you want, as long as you define the Start and End properly. Once all the values have been extracted, it returns the whole string without all the tags so you can display it as it should be.
Please note that I wrote this fast, I didn't include try-catch for invalid values. There may be easier ways to do this, but this should at least give you a lead.
I hope that helps!
Cheers,
~LegendBacon
Thank you for your reply! But I'm not really sure how I'd go about implementing this alongside the code from https://www.youtube.com/watch?v=_nRzoTzeyxU this video? Esp considering the string/char(?) is written in the inspector per character/object and whatnot?
I didn't watch the whole video, but from what I've seen it shouldn't be too complicated.
Since you already have a reference to the Text component in the IEnumerator TypeSentence(),
you could do sentence = ApplyDialogueLineTags(sentence);
before you go char by char. That way you get the sentence without the tags so it outputs properly. Of course, ins$$anonymous$$d of debugging the speed, fontsize etc you would apply it to your Text reference.
Ok! I'll try to implement it to the best of my ability, thank you so much for your help!