Hello everyone! I'm creating a game with a wind mechanic, so i need to display current wind power. There is a few wind values i need to check (power) and two variables used for wind side, with random chance i combine them and get one variable. I need to check in which side wind is going and display it to user. For that i used a switch statement, where i read wind power variable and display it to user depends on side and power, but it's kinda weird looking in code. Is another option exist for realising that? Hope for your help. There is my code: Code (CSharp): switch (switchVar) { case 0.005f: windText.text = "Easy wind to the Right"; break; case -0.005f: windText.text = "Easy wind to the Left"; break; case 0.010f: windText.text = "Medium wind to the Right"; break; case -0.010f: windText.text = "Medium wind to the Left"; break; case 0.015f: windText.text = "Strong wind to the Right"; break; case -0.015f: windText.text = "Strong wind to the Left"; break; case 0.020f: windText.text = "Very strong wind to the Right"; break; case -0.020f: windText.text = "Very strong wind to the Left"; break; default: windText.text = "No wind for now..."; break; } SwitchVar - my wind variable. If wind is negarive, it goes left, otherwise it goes right.
You just want to move away from the switch statement? Or am I misunderstanding? Code (csharp): public string GetWindPowerText(float windPower) { string windDirection = (windPower < 0) ? "Left" : "Right"; string windPowerText = ""; if (Mathf.Approximately(windPower, 0f)) { windPowerText = "No wind for now..."; } else { if (Math.Abs(windPower) <= 0.005f) { windPowerText = "Easy"; } else if (Math.Abs(windPower) <= 0.010f) { windPowerText = "Medium"; } else if (Math.Abs(windPower) <= 0.015f) { windPowerText = "Strong"; } else { windPowerText = "Very strong"; } windPowerText = $"{windPowerText} wind to the {windDirection}"; } return windPowerText; }
I wouldn't say it looks "weird" but it does seem needlessly verbose. You said that you combine info to get a single value so consider turning that value into a index. Seems to be 8 values so if you get them to be between 0 and 7 you can reference the correct string from an array and the switch disappears.
One option to simplify your code could be to use a dictionary to store the wind power values and their corresponding display text. Here's an example: Code (CSharp): // Create a dictionary to store wind power values and their corresponding display text Dictionary<float, string> windValues = new Dictionary<float, string>() { { 0.005f, "Easy wind to the Right" }, { -0.005f, "Easy wind to the Left" }, { 0.010f, "Medium wind to the Right" }, { -0.010f, "Medium wind to the Left" }, { 0.015f, "Strong wind to the Right" }, { -0.015f, "Strong wind to the Left" }, { 0.020f, "Very strong wind to the Right" }, { -0.020f, "Very strong wind to the Left" }, }; // Check if the wind value exists in the dictionary and display the corresponding text if (windValues.TryGetValue(switchVar, out string windTextValue)) { windText.text = windTextValue; } else { windText.text = "No wind for now..."; } With this approach, you can add new wind power values and their display text to the dictionary without having to modify the switch statement. Additionally, it may be easier to read and maintain the code compared to using a switch statement with many cases.