- Home /
String taken from a JSON file is not being Queued
So I'm working on a dialog system that is based in a JSON document. I managed to parse the information out of the file and into the console but it doesn't seem to want to get fed into the queue that I used to display the words and names. I really have no clue why. Any help would be appreciated.
This is the manager.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour {
[SerializeField]
public DialogueCollection dialogueCollection = new DialogueCollection();
public string dialogueFile;
public Text nameText;
public Text dialogueText;
public Animator animator;
public Animator leftChar;
public Animator rightChar;
public float textSpeed = 0.1f;
private Queue<string> sentences;
private Queue<string> names;
void Start() {
sentences = new Queue<string>();
names = new Queue<string>();
}
public void StartDialogue (Dialogue dialogue)
{
leftChar.SetBool("IsIn", true);
rightChar.SetBool("IsIn", true);
animator.SetBool("IsOpen", true);
TextAsset asset = Resources.Load(dialogueFile) as TextAsset;
if (asset != null)
{
dialogueCollection = JsonUtility.FromJson<DialogueCollection>(asset.text);
foreach (Dialogue JSON in dialogueCollection.Dialogue)
{
//This is where the information should be put in the queue.
print(JSON.name);
names.Enqueue(JSON.name);
print(JSON.words);
sentences.Enqueue(JSON.words);
}
sentences.Clear();
DisplayNextSentence();
}
else
{
Debug.Log("Dialog File is missing");
}
}
public void DisplayNextSentence()
{
print(sentences.Count);
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string name = names.Dequeue();
string sentence = sentences.Dequeue();
StopAllCoroutines();
nameText.text = name;
dialogueText.text = sentence;
StartCoroutine(TypeSentence(sentence));
Debug.Log(sentence);
}
IEnumerator TypeSentence (string sentence)
{
dialogueText.text = "";
foreach (char letter in sentence.ToCharArray())
{
dialogueText.text += letter;
yield return new WaitForSeconds(textSpeed);
}
}
void EndDialogue()
{
leftChar.SetBool("IsIn", false);
rightChar.SetBool("IsIn", false);
animator.SetBool("IsOpen", false);
Debug.Log("End of conversation");
}
}
This is the collection
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class DialogueCollection
{
public Dialogue[] dialogue;
public List<Dialogue> Dialogue = new List<Dialogue>();
}
This is the information take from the JSON
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue
{
public string name;
public string words;
}
The JSON
{
"Dialogue":
[
{
"name": "Wesley Walters",
"words": "Another quiet day, must be the 2nd week in a row without any cases."
},
{
"name": "Wesley Walters",
"words": "Not sure I’ll be able to make rent this month…"
},
{
"name": "Wesley Walters",
"words": "Might as well go make some coffee."
}
]
}
Answer by Bunny83 · Jul 13, 2020 at 12:21 PM
Well we can't tell you what exactly is wrong since we don't know how your json text looks like. However what seems really strange is that you have two collections / arrays of "Dialogue"s inside your "DialogueCollection" class but you only use the generic List with a capital "D" in your code while the other array with the lower case "d" seems to be completely ignored by your code. Do you actually have two arrays in your json data? What's their actual name?
Just to make this point clear. Your DialogueCollection class assumes json that looks like this:
{
"dialogue" : [],
"Dialogue" : [
{
"name" : "someName",
"words" : "some words"
},
{
"name" : "someOtherName",
"words" : "some more words"
}
]
}
As I said your "dialogue" array is not used by your code at all. You only care about your "Dialogue" array. You probably should check what your spelling of your key actually looks like and get rid of the other collection all together.
Finally it seems quite weird that you use two seperate queues for information that actually belongs together. Since that information is already packed into a class (your "Dialogue" class) it would make more sense to queue up those instances instead of the "name" and "words" seperately.
I didn't know if a queue could hold multiple different strings. I added the JASON above. It is not supposed to have two arrays. I think I fixed it as it is going into the Queue now, but I am still using 2 queues.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Modifying Brackeys' dialogue system for questions/different dialogue "paths"? 1 Answer
public Queue 2 Answers
dictionary and for each loop and for loop frustrating bug 1 Answer
foreach loop problem 1 Answer