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 jakieb · Nov 27, 2013 at 10:53 PM · inputif-statements

Branching Text help.

I am trying to write a text driven game just to teach myself a few things. I am wondering if there is a better way to handle a branching style of text. I am currently using "if" statements yo handle everything but I find it very confusing to get everything in order. Is there a better way to handle this? I know the code is necessarily going to function the way it is (most of it does) but I think you can see what I am trying to do with the displayed code.

Thank You!

activeText = a string that's displayed in the GUI.

 public void StateUpdate()
         {
 
             //The following is the Attack cycle.
 
             if   (Input.GetKeyDown(KeyCode.A) && attackStatus == 0)
             {
                 attackStatus = attackStatus +1;
             }
 
             if   (Input.GetKeyDown(KeyCode.A)  && attackStatus == 1)
             {
                 activeText = attackText;
             }
 
             if   (attackStatus == 2) // 
                                      
             {
                 activeText = attackText2;
             }
 
             //The following is the Look cycle.
 
             if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 0)
             {
                 lookStatus = lookStatus +1;
                 activeText = lookText;
             }            
 
             if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 1 && interactStatus == 0)
             {
                 activeText = lookText;
             }
             
             if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 1 && interactStatus == 1)
             {
                 activeText = lookText2;
 
             }
 
 //            if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 2 && interactStatus == 1);
 //            {
 //                activeText = lookText3;
 //            }
 
             //The following is the Interact cycle.
 
             if (Input.GetKeyDown(KeyCode.I) && lookStatus == 1)
             {
                 interactStatus = interactStatus+1;
                 activeText = interactText2;
 
             }
 
             if (Input.GetKeyDown(KeyCode.I) && lookStatus == 0)
             {
                 activeText = interactText;
             }
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
0
Best Answer

Answer by Spinnernicholas · Nov 27, 2013 at 11:42 PM

Option 1

You can try to use Switch statements for some of your code.

 if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 0)
 {
   switch(lookStatus)
   {
   case 0:
     lookStatus = lookStatus +1;
     activeText = lookText;
     break;         
   case 1:
     switch(interactStatus)
     {
     case 0:
       activeText = lookText;
       break;
     case 1:
       activeText = lookText2;
       break;
     }
     break;
   }
 }

You can also make methods to better organize things.

 if(Input.GetKeyDown(KeyCode.L))
 {
   look();
 }
 
 void look()
 {
   switch(lookStatus)
   {
   case 0:
     lookStatus = lookStatus +1;
     activeText = lookText;
     break;         
   case 1:
     switch(interactStatus)
     {
     case 0:
       activeText = lookText;
       break;
     case 1:
       activeText = lookText2;
       break;
     }
     break;
   }
 }

Lastly, You can use code folding with the "#region" directive.

 #region Attack
 //Stuff
 #endregion


Option 2

Create a class to handle it.

 class DialogState : Monobehavior
 {
   public String[] dialog;
   private int current = 0;
 
   String getCurrentDialog()
   {
     return dialog[current];
   }
 
   Bool next()
   {
     current++;
     if(current >= dialog.Length)
     {
       return false;
     }
     else
     {
       return true;
     }
   }

   void reset()
   {
     current = 0;
   }
 }

Then you can create GameObjects for each group of dialog and edit them in Unity.

Comment
Add comment · Show 2 · 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 Spinnernicholas · Nov 28, 2013 at 12:29 AM 0
Share

with option 2, you could make a dialog database like so:

 class DialogDatabase : $$anonymous$$onoBehavior, IDictionary<String, DialogState>
 {
   Dictionary<String,DialogState> dictionary;
 
   //wire IDictionary to dictionary....
 }

Then add a

 public String name;

field to the DialogState class.

  1. Create a gameobject and add DialogDatabase to it.

  2. Create separate gameobjects and add a DialogState to each of them.

  3. $$anonymous$$ake all the DialogStates children of the DialogDatabase.

  4. Create an entry in DialogDatabase for each DialogStates using it's name as a key.

  5. Tada, you can move the whole database of dialog around. You can store it as an asset and swap out entire databases on a whim.

I made this a comment because it is complex. $$anonymous$$ake sure you understand how generics and dictionaries work in C# before you rely too heavily on this code.

avatar image jakieb · Nov 28, 2013 at 12:53 AM 0
Share

That is awesome! Thanks so much. I will try this out tomorrow. I think this is exactly what I needed! I will come back and make sure I thumb up or let you know how it went.

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

17 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

Related Questions

Is there a reason GetKeyDown doesn't work with this code but GetKey does? 1 Answer

if statement error 1 Answer

Unity GUI Style with if Statements 1 Answer

imeCompositionmode 0 Answers

Can't call GetKey inside OnTriggerEnter? 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