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 /
  • Help Room /
avatar image
0
Question by Orion_Reed · Jan 14, 2017 at 02:46 PM · scripting problemsimplify

Looking for a way to simplify IF ELSE code.

I'm making a simple shortcut system to turn things like post-processing on and off, it works fine, but I can see that it will quickly turn into a complete mess. Here's the relevant snippet:

 void Update () 
     {
         if (Input.GetKeyDown (togglePostProcessing) && isPostProcessing == true) {
             player.profile = lowestQuality;
             isPostProcessing = false;
         } 
         else if (Input.GetKeyDown (togglePostProcessing) && isPostProcessing == false) {
             player.profile = mediumQuality;
             isPostProcessing = true;
         }
         if (Input.GetKeyDown (cinemaQualityOvveride)) {
             player.profile = highestQuality;
         }
     }

This is just two items. Eventually, I'll have a bunch of buttons, to toggle a bunch of features, and having one or two if(Input.GetKeyDown (button) && isFeatureActive) statements will get out of hand super quickly.

I'm hoping there's some way of condensing or simplifying this, so I can have dozens of shortcuts without hundreds of lines of confusing and repetitive code.

I've always been a fan of good systems, so if there's some way of making a script that deals with things like: 1. Toggling, so as to avoid a bunch of isThisFeatureEnabled bools 2. Input, so the actually important code isn't cluttered with IF INPUT statements

Any feedback is of course most welcome :)

Comment
Add comment · Show 6
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 QuentinLG · Jan 14, 2017 at 04:03 PM 0
Share

If you have to many if it's alway good to replace them by a switch statement.

avatar image Orion_Reed QuentinLG · Jan 14, 2017 at 04:52 PM 0
Share

Can a switch statement be used when I'm not comparing just one variable? Is there an elegant way of mapping, say, the F1-F9 keys to cases in a switch statement?

avatar image QuentinLG Orion_Reed · Jan 14, 2017 at 05:13 PM 0
Share

No, not really. There is a workaround apparently (see: https://forum.unity3d.com/threads/getkeydown-function-as-switch-case-instruction.5823/), but switch is mainly when the input is fixed, and with Get$$anonymous$$eyDown() you need to precise which key you are listening to, so it's not fixed.

avatar image QuentinLG · Jan 14, 2017 at 04:46 PM 0
Share

Also, as a general rule, isPostProcessing == true is redundant, isPostProcessing is already a boolean, there is no point comparing it to true or false, just remove the == true.

avatar image Orion_Reed QuentinLG · Jan 14, 2017 at 04:51 PM 0
Share

Very good point

avatar image Owen-Reynolds · Jan 14, 2017 at 04:59 PM 0
Share

Can break things out into functions: if(processingPressed) handleProcess$$anonymous$$eyPress();

That can make a long Update function more readable, and makes it easy to locate each section - collapse the code to where you just see the functions.

It's good for when you suspect the amount of work may grow. $$anonymous$$aybe you want to display the toggle status, flash a message, turn it into a 3-way ... . It's nice to be able to just grow to handleProcess$$anonymous$$ey function. Some people start with lots of 1-line functions like that, then about 1/2 grow to 3 lines, and a few grow huge.

1 Reply

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

Answer by Oussama-Gammoudi · Jan 14, 2017 at 03:41 PM

 void Update()
 {
     if (Input.GetKeyDown(togglePostProcessing)) {
         player.profile = isPostProcessing ? lowestQuality : mediumQuality;
         isPostProcessing = !isPostProcessing;
     }
     if (Input.GetKeyDown(cinemaQualityOvveride))
     {
         player.profile = highestQuality;
     }
 }

To keep the exact same behavior this is as far as it can get.

 if (Input.GetKeyDown(togglePostProcessing))
     player.profile = (isPostProcessing=!isPostProcessing) ? mediumQuality :lowestQuality;
 if (Input.GetKeyDown(cinemaQualityOvveride))
     player.profile = highestQuality;
Comment
Add comment · Show 3 · 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 Orion_Reed · Jan 14, 2017 at 04:12 PM 0
Share

This is fantastic and indeed more compact, is this the optimal way of structuring the script I'm writing? As in, is having a list of IF statements in Update() the best way to go about this?

avatar image Orion_Reed Orion_Reed · Jan 14, 2017 at 04:54 PM 0
Share

Can this be compacted into one line with the ? operator? Something like player.profile = player.profile = mediumQuality ? lowestQuality : mediumQuality; But that doesn't set the quality back to medium...

avatar image Oussama-Gammoudi Orion_Reed · Jan 14, 2017 at 07:44 PM 0
Share

I've updated it with shorter version

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

64 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

Related Questions

Best way to simplify this code 1 Answer

How to move space shooter player 1 Answer

Need help to reduce my script 0 Answers

Script for activating next GameObject after first one has been found/tracked (Vuforia) 1 Answer

Prefab Scripts Not Working After First Spawn? 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