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 :)
If you have to many if
it's alway good to replace them by a switch statement.
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?
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.
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
.
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.
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;
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?
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...