Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 conguerror · Apr 30, 2020 at 12:37 PM · arraytextbuttonsindexindexing-array

DIALOGUE BRANCH CREATION?

Script is giving me headache and bugs(like index out of bound and chaotic answers and replies) It's just changing text components for now really. Relevant methods:

 public string[] dialogueBranch1;//changing textbox
         public string[] dialogueBranch2;
         public string[] dialogueBranch3;
         int branch1Index = 0;
         int branch2Index = 0;
         int branch3Index = 0;
         public string[] answerBranch1;
         public string[] answerBranch2;
         public string[] answerBranch3;
 
 public void ChoiceOption1()
     {
         textBox.GetComponent<Text>().text = dialogueBranch1[branch1Index];
         choiceMade = 1;
     }
     public void ChoiceOption2()
     {
         textBox.GetComponent<Text>().text = dialogueBranch2[branch2Index];
         choiceMade = 2;
     }
     public void ChoiceOption3()
     {
         textBox.GetComponent<Text>().text = dialogueBranch3[branch3Index];
         choiceMade = 3;
        
     }
         public void ChoiceExit()
         {
             HideDialogue();
             ShowCanvas();
             isTalking = false;
             
         }
 
         private void Update()
         {
             if (isTalking == true)
             {
                 Cursor.lockState = CursorLockMode.None;
                 Cursor.visible = true;
             }
             else
             {
                 Cursor.lockState = CursorLockMode.Locked;
                 Cursor.visible = false;
             }
 
 
             if (choiceMade == 1)
             {
                 ClickChanger(choice1, answerBranch3[branch1Index]);
             }
             if (choiceMade == 2)
             {
                 
                 ClickChanger(choice2, answerBranch2[branch2Index]);
 
             }
             if (choiceMade == 3)
             {
                 ClickChanger(choice3, answerBranch3[branch3Index]);
                 
             }
         }
 private void ClickChanger(GameObject buttonGameObject, string txt)
         {
             //print a new text on button and text box(replace buttons)
             buttonGameObject.GetComponent<Text>().text = txt;
             buttonGameObject.GetComponent<Button>().onClick.AddListener(ChangeTextForChoice);
         }
         void ChangeTextForChoice()
         {
             if(choiceMade == 1)
             {
                 SetText(answerBranch1[branch1Index]);
                 if (branch1Index <= dialogueBranch1.Length)
                 {
                     branch1Index++;
                 }
                 else
                 {
                     return;
                 }
             }
             if (choiceMade == 2)
             {
                 SetText(answerBranch1[branch2Index]);
                 if (branch2Index <= dialogueBranch2.Length)
                 {
                     branch2Index++;
                 }
                 else
                 {
                     return;
                 }
             }
             if (choiceMade == 3)
             {
                 SetText(answerBranch1[branch3Index]);
                 if (branch3Index <= dialogueBranch3.Length)
                 {
                     branch3Index++;
                 }
                 else
                 {
                     return;
                 }
             }
 
             //will change it
         }
 
         void SetText(string txt)
         {
             textBox.GetComponent<Text>().text = txt;
         }

Maybe I should use dictionaries?

Comment
Add comment · Show 1
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 conguerror · Apr 30, 2020 at 12:38 PM 0
Share

choice1,choice2,choice3 variables are buttons by the way

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Thomas-Hawk · May 01, 2020 at 11:49 PM

Here ya go, this is from a pet project involving some visual novel jazz. I dont have time to really clean it up but, the concepts shown should help you.

Define dialogues and responses as structs, basically. Then, the number of options or responses or whatever can be dynamically defined in the editor.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class ConversationHandler : MonoBehaviour
 {
     public Transform dialoguePanel;
     public Transform responsePanel;
     public GameObject ourOptionPrefab;
     public GameObject responseDialoguePrefab;
     //define the struct for a dialogue option
     [System.Serializable]
     public struct dialogueOption
     {
         public string text;
         public int convo;
         public int response;
         public bool onlyShowOnce;
 
         public bool branchResponse;
         responseDialogue singleResponse;
 
 
         
     }
     [System.Serializable]
     public struct responseDialogue
     {
         public string text;
         public List<int> responses;
         public int actorImage;
     }
     [System.Serializable]
     public struct actor
     {
         public MeshRenderer image; 
         public List<Texture> graphics;
     }
     [System.Serializable]
     public struct conversation
     {
         public string convoName;
         public actor main_actor;
         public List<responseDialogue> responses;
         public List<dialogueOption> dialogues;
     }
 
     public List<conversation> conversations;
 
     
     public int testConvo;
     public void PopGUI(int convo)
     {
         Respond(0, 0);
 
 
     }
 
     public void ClearScreen()
     {
 
         /*
         foreach (Transform child in dialoguePanel)
         {
             print("Destroying " + child.name);
             DestroyImmediate(child.gameObject);
         }
         foreach (Transform child in responsePanel)
         {
             print("Destroying " + child.name);
             DestroyImmediate(child.gameObject);
         }
        */
 
 
         int childs = dialoguePanel.childCount;
         for (int i = childs - 1; i >= 0; i--)
         {
             DestroyImmediate(dialoguePanel.GetChild(i).gameObject);
         }
 
 
         int childs1 = responsePanel.childCount;
         for (int i = childs1 - 1; i >= 0; i--)
         {
             DestroyImmediate(responsePanel.GetChild(i).gameObject);
         }
     }
 
 
     
     public void Respond(int convo, int dOption)
     {
         print("Responding for option " + dOption + " in convo " + convo);
         dialogueOption option = conversations[convo].dialogues[dOption];
         print("option is " + option.text);
         //this function needs to clear the currently displayed things
         ClearScreen();
         //okay, the dialogue options and response options are destroyed.
         //for the option given, load the response
         responseDialogue showResponse = conversations[convo].responses[option.response];
         GameObject newResponse = Instantiate(responseDialoguePrefab, responsePanel);
         newResponse.GetComponentInChildren<Text>().text = showResponse.text;
         for (int i = 0; i < (showResponse.responses.Count); i++) {
             print(i);
             
             //  Debug.Break();
             GameObject newOption = Instantiate(ourOptionPrefab, dialoguePanel);
             print("new Option name " + newOption.name);
             DialogueOption dialogue = newOption.GetComponent<DialogueOption>();
             dialogue.keyPress = i + 1;
             dialogue.option = conversations[convo].dialogues[showResponse.responses[i]];
             dialogue.GetComponentInChildren<Text>().text = dialogue.option.text + "./"  +i;
 
 
             if (i >= showResponse.responses.Count)
             {
 
                 print("wat");
             }
           //  DestroyImmediate(newOption);
         }
 
 
         //okay here we should be able to set the actor for this conversation to have a new texture
 
 
         MeshRenderer rend = conversations[convo].main_actor.image;
         Material mat = rend.sharedMaterial;
         mat.mainTexture = conversations[convo].main_actor.graphics[showResponse.actorImage];
         rend.sharedMaterial = mat;
         conversations[convo].main_actor.image.material = rend.material;
 
 
     }
 
     public void TestPaths()
     {/*
         for (int c = 0; c<conversations.Count; c++)
         {
             conversation convo = conversations[c];
             for (int r = 0; r < convo.responses.Count; r++)
             {
                 responseDialogue response = convo.responses[r];
                 bool accessable = false;
                 foreach(int i in response.responses)
                 {
                     if (i == r)
                     {
                         accessable = true;
                     }
 
                 }
                 if (!accessable)
                 {
                     print("Not accessable on response " + r);
                 }
 
             }
 
         
         }*/
 
 
         foreach(conversation convo in conversations)
         {
             bool test = false;
             for (int r = 0; r<convo.responses.Count; r++)
             {
                 bool accessible = false;
                 //foreach (int rt in convo.responses[r].responses)
                // {
                    // if (t.response == )
                    //is this convo accessile from any dialogue?
                    //does r match the resonse for any dialogue?
 
                         for (int d = 0; d < convo.dialogues.Count; d++)
                         {
                             print("testing response " + r + " across dialogue " + d);
                                if(convo.dialogues[d].response == r)
                                {
                                    accessible = true;
                                    print("dialogue " + d+ "  matched response "+ r + " for " +  convo.dialogues[d].response);
                                }
 
                            // dialogueOption t = convo.dialogues[d];
 
 
                             //for each of the dialogues from each response,
                             //run through
                             /*
                           foreach(int t in convo.responses[r].responses)
                             {
                                 if (t == r)
                                     accessible = true;
 
                             }*/
 
                         }
                 //}
 
                 if (!accessible)
                 {
                     print("Warning! No dialogues point to this response:  " + r);
                     test = true;
                 }
 
 
             }
             if (!test)
             {
                 print("This conversation passed all path checks: " + convo.convoName);
             }
 
 
         }
     }
 
 
 
 }
 

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 conguerror · May 02, 2020 at 02:39 PM 0
Share

Thank you very much. Hope it will help

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

149 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change button text via script? 2 Answers

Arrays rebels to my power! 1 Answer

The index value is not starting from 0 0 Answers

Comparing each random element in an array with each other element 3 Answers

Array Index Out of Range 2 Answers


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