Substring vs string array

I’m creating a simple dialogue system. I have 2 methods in mind, but I’m unsure if using string.Substring is feasible at all?

All dialogues would be written on a simple doc or a separate node base text editor, so I just copy the text over without bloating Unity.

  1. _mainDialogueIndex[ ] drives all the text before a dialogue option comes up.
  2. strings[ ] are either all lines before options, or just one continuous string using substring to break line.
  3. dialogueOptions[ ] is just # of options to trigger a method or bool based on option[index] called.
  4. _Goto() decides which _mainDialogueIndex[ ] to go next.

so at 2) is string.Substring even an option since no tutorial or any dialogue system I’ve seen ever mention the use of string.Substring?

You could definitely use substring if you wanted, or split, but it feels like extra processing without a lot of benefits. How are you storing the dialogue options in the first place?

I’m probably going to use ScriptableObject to store all the data. Then just pull strings from there.

I guess substring would need to loop through the string each time it continues the dialogue…

I’m trying to find a way to not break up the paragraph into so many strings/files that it becomes hard to keep track later on. So I thought perhaps string.Substring would allow me to paste the entire paragraph into one text area.

If all you want is to load text then maybe use a TextAsset instead of SO. That way you could still edit it easily with an external editor later on. Are you going to localize those strings? If yes then you will most likely be better off storing keys instead of the actual text.
If you use SO you could use typed values to manage your method parameters (no need to parse those as well).
You could parse the strings and cache the result for later use. That way you wouldn’t have to reparse multiple times.

1 Like

Sorry I’ve never done localization before, what’s the difference between storing a key, and directly translating the string?

Thanks I’ll look into that!

I managed to create a quick dialogue system using substring for anyone coming across this post.
Screenshots

The code is really simple. No bloated drop down strings in inspector, or the use of nodes. Anyone can paste a large body of text and it breaks into different lines using defined separators (in my case [line]).

using UnityEngine;
using TMPro;

public class DialogueSystem : MonoBehaviour
{
    [TextArea(4,10)]
    [SerializeField] string _dialogue;
    [SerializeField] string[] _lines;
    [SerializeField] string[] _separators;
    [SerializeField] TMP_Text _dialogueUI;
    [SerializeField] GameObject _buttonUI;
    [SerializeField] TMP_Text _buttonUIText;

    int _currentLine;

    private void Start()
    {
        _dialogue = "This is line 1 and the world is starting." +
            "[line]This is line 2 and the world is still." +
            "[line]This is line 3 and the world is still still." +
            "[line]This is line 4 and the world is almost ending." +
            "[line]This is line 5 and the world is ending." +
            "[line]This is line 6 and the world ended.";

        _lines = _dialogue.Split(_separators, 10, System.StringSplitOptions.None);

        _dialogueUI.text = _lines[_currentLine];
    }

    public void AdvanceLine()
    {
        if(_currentLine < _lines.Length - 1)
        {
            _buttonUIText.text = "continue";

            _currentLine++;

            _dialogueUI.text = _lines[_currentLine];
        }
        else
        {
            _dialogueUI.gameObject.SetActive(false);
            _buttonUI.SetActive(false);
        }

    }
}

I’m not sure how you are trying to use Substring here…

From your goal it seems that you can use JSON or XML formatting for dialogues (to use JSON is simpler from code perspective).
JSON looks like this:

So, you store all your tree of dialogues in JSON format and then you just load it in Unity to some class hierarchy.
To edit your dialogues you can use something like that https://jsoneditoronline.org/
And this is how you load it from Unity Unity - Scripting API: JsonUtility.FromJson

So, something like this for your case


and to have multiple lines, you can do like this
“Text”: “First line\nSecond line\nThird line”

2 Likes

Maybe you didn’t get to see the post above before posting this. But the substring is used to break up a large body of text into dialogue lines as seen in the post above.

Most dialogue trees don’t split until presented an option, so if that’s the case, why should I bloat the interface with dropdowns of a single string when I should just be able to break one string into different lines?

That’s why I suggested if using substrings would be feasible since no tutorials I’ve come across ever mentions breaking one big body of text into lines instead of one string per line.

Basically what you do instead of storing a text like “The brown fox jumped over the river” directly in your game you only store a “key” as replacement, just like a variable name. In this case it could be “FoxRiverJumpDialogLine1”. Then you setup a table (xls, csv, xml, json, SOs, … however you like) and there you put in the key and the text in the same row. The advantage is that you can store multiple texts for the same key (multiple languages). And all your texts are in one location (easy to handle for translators, spell checking etc.). In your dialog system you would then only refer to these keys, not the actual text.

Here is a thread where another user asked about how to do it. I explained my approach there: https://forum.unity.com/threads/game-texts-how-do-you-manage-yours.1140085/

1 Like