Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jaminath · Jul 13, 2020 at 06:04 AM · unity 2dlistforeachdialoguequeue

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."
         }
     ]
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jaminath · Jul 13, 2020 at 04:42 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges