Best Practice For Storing RPG Dialogue?
I am working on an RPG dialogue system where each dialogue line consists of the actual dialogue being spoken, the name of the speaker, and a facial expression for the speaker, if applicable. I have done this by creating the following Dialogue class and creating a public Dialogue list within a DialogueHolder class, allowing me to edit dialogue within the editor.
[System.Serializable]
public class Dialogue {
public string name;
public Sprite emote;
[TextArea] public string line;
public Dialogue(string n, string l, Sprite e)
{
name = n;
emote = e;
line = l;
}
}
However, on a previous project, I used a different approach for storing dialogue. I used a struct, named DialogueLine, instead of a class, and loaded each line from a text file, using semicolons to separate information.
public void LoadDialogue(TextAsset textToLoad)
{
int i = 0;
string[] textLines;
// Separate the text into lines
textLines = (textToLoad.text.Split('\n'));
// Add each line to the list
do
{
string[] lineData = textLines[i].Split(';');
DialogueLine lineEntry = new DialogueLine(lineData[0], lineData[1], lineData[2][0]);
lines.Add(lineEntry);
i++;
} while(i < textLines.Length);
}
What I want to know is which of these two methods is better: storing dialogue within the editor or using external text files and reading them in. I can already see some pros and cons of each approach in regards to ease of use, but is there something I'm not factoring in (i.e. performance) that should convince me to chose one over the other?
Answer by Harinezumi · Feb 13, 2018 at 10:52 AM
Depending on the size of the project, if it is really small, then just set up dialogues from the editor. In any other case, you will have benefits from loading the dialogue from a resource file (even in case it is small, but then it might not be worth the extra effort).
But what I would do is to make a mixed system: being able to set up from the Editor, but being able to override the setup from the text file. That way you will get the flexibility of quickly changing, testing things if needed, but also the advantages of defining from a text file.
As for using class or struct, there is not much difference between them except for a class being a reference type (can be null, can be modified when passed as normal parameter), while a struct is not.
What would constitute a small project? Do you mean the number of people working on it, which in this case is just me, or are you referring to the length of the game itself?
The length of the game, the number of scenes, but I think the amount of dialogue texts is the best measurement in this case. So if you have only a few dialogues (maybe not more than 30 lines - but this is just my "measurement"), then don't worry about setting up loading it from file. Anything above that I think you would benefit more from it (in the long run). And if you make it general enough, maybe you can reuse it in another game, or even sell it in the asset store ;)
So I ended up creating a system that implements both methods like you said. I check for a public TextAsset in Start(), and if it isn't null I replace any publicly set dialogue with the data from that TextAsset. It loads the name and dialogue properly, but I haven't implemented anything for the facial expressions, as I was previously setting them using public Sprites. What I plan on doing is putting all of my facial expressions into an array, and then reading an index from the text file (i.e. if Happy Boy's face is at index 2 in Sprite[] faces, then use 2 as the face parameter for Happy Boy's lines).
Thank you so much for the help!