Search Unity

Resolved Switch statement

Discussion in 'Scripting' started by Sesorot, Mar 1, 2023.

  1. Sesorot

    Sesorot

    Joined:
    Dec 1, 2022
    Posts:
    2
    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):
    1. switch (switchVar)
    2. {
    3.     case 0.005f:
    4.         windText.text = "Easy wind to the Right";
    5.         break;
    6.     case -0.005f:
    7.         windText.text = "Easy wind to the Left";
    8.         break;
    9.     case 0.010f:
    10.         windText.text = "Medium wind to the Right";
    11.         break;
    12.     case -0.010f:
    13.         windText.text = "Medium wind to the Left";
    14.         break;
    15.     case 0.015f:
    16.         windText.text = "Strong wind to the Right";
    17.         break;
    18.     case -0.015f:
    19.         windText.text = "Strong wind to the Left";
    20.         break;
    21.     case 0.020f:
    22.         windText.text = "Very strong wind to the Right";
    23.         break;
    24.     case -0.020f:
    25.         windText.text = "Very strong wind to the Left";
    26.         break;
    27.     default:
    28.         windText.text = "No wind for now...";
    29.         break;
    30. }
    SwitchVar - my wind variable. If wind is negarive, it goes left, otherwise it goes right.
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,153
    You just want to move away from the switch statement? Or am I misunderstanding?

    Code (csharp):
    1. public string GetWindPowerText(float windPower)
    2. {
    3.     string windDirection = (windPower < 0) ? "Left" : "Right";
    4.     string windPowerText = "";
    5.  
    6.     if (Mathf.Approximately(windPower, 0f))
    7.     {
    8.         windPowerText = "No wind for now...";
    9.     }
    10.     else
    11.     {
    12.         if (Math.Abs(windPower) <= 0.005f)
    13.         {
    14.             windPowerText = "Easy";
    15.         }
    16.         else if (Math.Abs(windPower) <= 0.010f)
    17.         {
    18.             windPowerText = "Medium";
    19.         }
    20.         else if (Math.Abs(windPower) <= 0.015f)
    21.         {
    22.             windPowerText = "Strong";
    23.         }
    24.         else
    25.         {
    26.             windPowerText = "Very strong";
    27.         }
    28.  
    29.         windPowerText = $"{windPowerText} wind to the {windDirection}";
    30.     }
    31.  
    32.     return windPowerText;
    33. }
     
    Sesorot likes this.
  3. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    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.
     
  4. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,375
    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):
    1. // Create a dictionary to store wind power values and their corresponding display text
    2. Dictionary<float, string> windValues = new Dictionary<float, string>()
    3. {
    4.    { 0.005f, "Easy wind to the Right" },
    5.    { -0.005f, "Easy wind to the Left" },
    6.    { 0.010f, "Medium wind to the Right" },
    7.    { -0.010f, "Medium wind to the Left" },
    8.    { 0.015f, "Strong wind to the Right" },
    9.    { -0.015f, "Strong wind to the Left" },
    10.    { 0.020f, "Very strong wind to the Right" },
    11.    { -0.020f, "Very strong wind to the Left" },
    12. };
    13.  
    14. // Check if the wind value exists in the dictionary and display the corresponding text
    15. if (windValues.TryGetValue(switchVar, out string windTextValue))
    16. {
    17.    windText.text = windTextValue;
    18. }
    19. else
    20. {
    21.    windText.text = "No wind for now...";
    22. }
    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.
     
    Sesorot and AngryProgrammer like this.
  5. Sesorot

    Sesorot

    Joined:
    Dec 1, 2022
    Posts:
    2
    Wow! Thank you so much! I should learn about dictionaries more!