Iterate through dialogue array and display in game
What end product am I trying to archive:
I am making a Walking Sim in which different characters talk whenever a certain event is triggered.
(Sometimes when you walk past a certain place on the map, sometimes when you pick or interact with an item and sometimes after a certain time).
The Dialogue is played in a set order.
There is, however, one option to skip a part of the story.
What I have so far:
A Dialogue Script:
[System.Serializable] public class Dialogue
{
[SerializeField]
private string nameOfSpeaker; //Adding the name of the current speaker in the Unity Inspector
[SerializeField]
[TextArea(3,10)]
private string sentence; //Adding the displayed sentence the speaker says in the Unity Inspector
}
A Dialogue Manager:
public class DialogueManager : MonoBehaviour
{
private Dialogue dialogue;
//Adding a variable from type Dialogue to be able to access it's variables (See "Dialogue"-Script for
reference)
List<Dialogue[]> chapters = new List<Dialogue[]>(); //Making a List of Dialogue Arrays
/*Since the player will be able to skip a part of the dialogue
* we added divided the story into chapters and
* made them seperate arrays. ChapterOne has to be played,
* ChapterTwo is skippable and ChapterThree is the ending, wheather or not
* the second chapter is skipped*/
[SerializeField]
private Dialogue[] chapterOne;
[SerializeField]
private Dialogue[] chapterTwo;
[SerializeField]
private Dialogue[] chapterThree;
[SerializeField]
private TMP_Text text; //drag and drop the TMPText that should display the current dialogue sentence
private void OnEnable() //subscribing to the Event that triggers the current Dialogue[i] to be played
{
Message<ContinueDialogueEvent>.Add(OnContinueDialogue);
}
private void OnDisable()//unsubscribing to the Event that triggers the current Dialogue[i] to be played
{
Message<ContinueDialogueEvent>.Remove(OnContinueDialogue);
}
private void Start() //Adding the Chapters to the List
{
chapters.Add(chapterOne);
chapters.Add(chapterTwo);
chapters.Add(chapterThree);
}
public void StartDialogue (Dialogue dialogue)
{
Debug.Log(message: "Started a conversation");
}
private void OnContinueDialogue(ContinueDialogueEvent ctx) //See "DialogueTrigger"-Script for reference
{
Debug.Log("Continue Dialogue in Chapter " + ctx.Chapter);
}
}
A DialogueTrigger (not Sure if this is important):
public class DialogueTrigger : MonoBehaviour
{
[SerializeField] private bool isTriggered; //Checking weather or not the Dialogue has been triggered
[SerializeField] private bool wasExecuted; //Checking weather or not it has already been executed
[SerializeField] private int chapter; //Chapter 1-3
private void OnTriggerEnter(Collider other) //Triggering Dialogue through walking through an invisible collider
{
if (!wasExecuted)
{
if (isTriggered)
{
NextDialogue();
}
}
}
public void NextDialogue()
{
if (!wasExecuted)
{
Message.Raise(new ContinueDialogueEvent(chapter));
}
wasExecuted = true;
}
}
What I need help with:
I wanted to add a Variable (index) that is raised by one everytime a Dialgoue is triggered and then use this Variable to display the next Dialogue (nameOfSpeaker and Sentence) in the Array. Lets assume we just want to do all that with ChapterOne[].
Questions:
How do I display the Dialogue (the nameOfSpeaker and Sentence) in Unity (I am using TMP)?
How do I write this Method best?
I have been watching a lot of Dialogue Tutorials yet none seems to help me with my exact case.
I think the solution to this problem is quite easy compared to what I have already done, but I have been staring at this for too long and do not know how to continue.
Info: I am neither well experienced in Unity nor coding.
Answer by Dsiak · Nov 24, 2021 at 12:46 AM
Maybe you would like to consider using a Queue data type instead of a list? Them you can just Dequeue the next sentence and pay no attention to indexes. If that doesn't sound appealing you probably going to need to create some sort of "chapterOneIndex" and have "NextDialogue" update the indexes accordingly. As for displaying the text you need to create two variables to store the text mesh pro object and set it's text to the sentence, something like this:
using TMPro; //Using text mesh pro namespace
public TextMeshProUGUI dialogueText, nameOfSpeakerText; // Drag and drop the two texts elements using the inspector
public void NextDialogue(){
nameOfSpeakerText.text = dialogue.nameOfSpeaker
dialogueText.text = dialogue.sentence
}