Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Soft_Ordnance · Feb 26, 2017 at 10:23 PM · switchcasebad

Is it bad coding to have a switch statement with 300 cases in update()?

I want to display a sequence of paragraphs, a new paragraph with every button press. Would it be inefficient to have a switch statement with (maybe) 300 cases in the update function? An example of how it would work:

 // a button is pressed that changes a bool variable to true
 if bool variable is true
 
 switch (paragraph number variable)
 case: paragraph 01
 display paragraph 02
 change bool variable to false
 break

 case: paragraph 02
 display paragraph 03
 change bool variable to false
 break

and so on.

Comment
Add comment · Show 3
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 NecrosDk · Feb 26, 2017 at 10:39 PM 0
Share

Can you try to describe the problem you're trying to solve a bit better? then we might be able to come up with a solution.

avatar image NecrosDk · Feb 26, 2017 at 10:53 PM 2
Share

Also if I'm guessing correctly here, you want to cycle between text segments, one after the other, based on a key press.

An easy way to achieve this is to have all your text segments in a string array, and have an integer keep track of how far into the conversation you are. Something like this:

void NextTextSegment(){

textField.text = arrayOfParagraphs[currentParagraph];

currentParagraph++;

}

So yeah if you look at it that way, your first proposed solution is pretty inefficient.

avatar image Soft_Ordnance NecrosDk · Feb 28, 2017 at 06:55 AM 0
Share

Oh yeah i can call functions from Unity (not sure if those are the right terms). Hah i've got a long way to go.

1 Reply

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

Answer by CarterG81 · Feb 26, 2017 at 11:30 PM

Yea, definitely.

There are nearly always better ways to code "300 cases" than using a switch. Most programmers don't use switches larger than 5-10.

Part of what makes a good programmer is their ability to write readable code & code that is easily extendable. Having to sift through hundreds of conditionals is not user friendly.

If you're just iterating through paragraphs, you should simply use a loop of some kind. Iterate through an array, List, dictionary, whatever. There are usually better ways than enormous switch statements, like making a generic method or approaching the problem in a completely different way. Just as NecrosDk suggests.

If you tell more details, like how you write your content, we could better help answer the question. But really the answer is "Yes. It is bad coding." However it does work - it's just not a good way to code.

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 Soft_Ordnance · Feb 28, 2017 at 02:15 AM 0
Share

Well i want to make a working prototype with paragraphs showing up in sequence before attempting to make a text adventure game. Perhaps with branching paths that might loop back to the main story thread. Although looking at the problem now I think I'm going to have to study program$$anonymous$$g a bit more.

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

6 People are following this question.

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

Related Questions

Operator '==' cannot be used! 2 Answers

Getting dynamic indicies from multiple dropdown menus 0 Answers

Switch case using enum javascript 2 Answers

Why does only one object respond, while more objects have exact same script? 0 Answers

Saving drop down menu selection 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