Search Unity

Setting Int To Customized String Of My Choosing

Discussion in 'Scripting' started by EternalAmbiguity, May 30, 2015.

  1. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    I'm using Unistorm, and I'm trying to display the weather as a part of the UI. I've got it in, but the problem is it's just a bunch of numbers. 1-11 or something.

    So what I need to do is convert the int values into a customize-able string, so that I can label weather type "1" "foggy" instead, and so on. This doesn't condense down into simple terms very easily so I didn't find anything about it online. Can anyone help me here?

    I imagine it would be done in some way by setting the int equal to a particular string, but I'm not seeing anything about that online either so I really do not know how this would be done.
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Not sure what you're really wanting to do but there are many possible ways. Here are a couple of examples:

    Code (csharp):
    1. string GetWeatherName(int weatherNumber)
    2. {
    3.   switch(weatherNumber)
    4.   {
    5.     case 0:
    6.       return "freezing";
    7.     case 1:
    8.       return "almost freezing";
    9.     case 2:
    10.       return "may as well be freezing";
    11.     case 3:
    12.       return "feels very cold";
    13.     case 4:
    14.       return "eh";
    15.     default:
    16.       return "warmer than 4";
    17.   }
    18. }
    Or something like:
    Code (csharp):
    1. private string[] weatherNames = { "freezing", "almost freezing", "may as well be freezing", "feels very cold",  "eh"};
    2.  
    3. string GetWeatherName(int weatherNumber)
    4. {
    5.   if(weatherNumber < 0)
    6.   {
    7.     return weatherNames[0];
    8.   }
    9.  
    10.   if(weatherNumber >= weatherNames.Length)
    11.   {
    12.      return "So hot we done even have a word for this";
    13.   }
    14.  
    15.   return weatherNames[weatherNumber];
    16. }
    Is this the sort of thing you're wanting?
     
    EternalAmbiguity likes this.
  3. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    The first one looks like what I want, thanks.

    However, I'm pulling the int from a different script, with this line here:

    Code (csharp):
    1.  
    2. WeatherButton.GetComponentInChildren<Text>().text = UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().weatherForecaster.ToString();
    3.  
    How exactly would I incorporate what you describe into that? Would I simply call the component I'm using (weatherForecaster in my case) as opposed to displaying it, then use your top script (changing the string and int name of course), and then display the string somehow?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Enums. Enums are designed for allowing words to be used instead of numbers.
     
  5. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Would this be more straightforward than the method put forward by Iarku? If so, can you link to a relatively simple explanation of enums (or if you're feeling particularly magnanimous explain it yourself) that would show how I would implement it in displaying to the UI?
     
    Last edited: May 30, 2015
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    Enums are good for going from words to numbers but less effective at going form numbers to words. The reason is because Enums are symbols in the C# language and symbols cannot contain certain characters, such as the space.

    My preference of the three is @larku 's second suggestion.
     
  7. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I'll guess that:

    UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().weatherForecaster

    Is returning an int?

    If so it'd just be something like:

    Code (csharp):
    1.  
    2. WeatherButton.GetComponentInChildren<Text>().text = GetWeatherName(UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().weatherForecaster);
    3.  
    ??
     
    EternalAmbiguity likes this.
  8. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Well thanks for all the help.

    I've tried to incorporate the first suggestion into my script, and it looks like this:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Temp : MonoBehaviour {
    6.  
    7.     public GameObject UniStormWeatherScript;
    8.     private GameObject TempTellButton;
    9.     private GameObject WeatherButton;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         UniStormWeatherScript = GameObject.Find("UniStormSystemEditor");
    14.         TempTellButton = GameObject.FindWithTag ("TempButton");
    15.         WeatherButton = GameObject.FindWithTag ("WeatherButton");
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         string GetWeatherName(int weatherForecaster)
    23.         {
    24.             switch(weatherForecaster)
    25.             {
    26.             case 0:
    27.                 return "freezing";
    28.             case 1:
    29.                 return "almost freezing";
    30.             case 2:
    31.                 return "may as well be freezing";
    32.             case 3:
    33.                 return "feels very cold";
    34.             case 4:
    35.                 return "eh";
    36.             case 11:
    37.                 return "Ooh!";
    38.             default:
    39.                 return "warmer than 4";
    40.             }
    41.         }
    42.  
    43.         TempTellButton.GetComponentInChildren<Text>().text = UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().hourCounter + ":" + UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().minuteCounter;
    44.         WeatherButton.GetComponentInChildren<Text>().text = GetWeatherName(UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().weatherForecaster);
    45.  
    46.  
    47.  
    48.     }
    49.  
    50. }
    51.  
    It's got some issues though. For some reason I get an "unexpected symbol" error at the "string GetWeatherName(int weatherForecaster)" line, and then for every "case" and "default." It also messes up the "TempTellButton" line near the bottom, even though it has nothing to do with that.

    I can move the string-int-switch section down below the two UI button lines, but then "GetWeatherName" in the bottom one doesn't work. Any suggestions? Thanks again for the help.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code (CSharp):
    1. public enum WeatherType {snowy = 0, frosty =1}
    2.  
    3. public void CheckWeather (WeatherType weather) {
    4.     if (weather = WeatherType.snowy) {
    5.         Debug.Log("It's cold");
    6.     }
    7. }
    8.  
    9. // You can also do
    10.  
    11. int myInt = (int) WeatherType.snowy;
    12. WeatherType weather = (WeatherType) 1;
     
    EternalAmbiguity likes this.
  10. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Well hmm. I thank you for your help, but I'm very much a noob. Here's my code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class Temp : MonoBehaviour {
    7.  
    8.     private GameObject UniStormWeatherScript;
    9.     private GameObject TempTellButton;
    10.     private GameObject WeatherButton;
    11.  
    12.     // You can also do
    13.  
    14.     //int myInt = (int) WeatherType.snowy;
    15.     //WeatherType weather = (WeatherType) 1;
    16.  
    17.     // Use this for initialization
    18.  
    19.     void Start () {
    20.         UniStormWeatherScript = GameObject.Find("UniStormSystemEditor");
    21.         TempTellButton = GameObject.FindWithTag ("TempButton");
    22.         WeatherButton = GameObject.FindWithTag ("WeatherButton");
    23.  
    24.         int weather = UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().weatherForecaster;
    25.          
    26.     }
    27.  
    28.     public enum WeatherType {snowy = 0, frosty = 1, nothing = 6}
    29.      
    30.         public void CheckWeather (WeatherType weather) {
    31.             if (weather == WeatherType.snowy) {
    32.                 Debug.Log("It's cold");
    33.             }
    34.          
    35.             if (weather == WeatherType.nothing){
    36.                 Debug.Log ("It's nothing");
    37.             }
    38.         }
    39.  
    40.     // Update is called once per frame
    41.     void Update () {
    42.  
    43.  
    44.         TempTellButton.GetComponentInChildren<Text>().text = UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().hourCounter + ":" + UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().minuteCounter;
    45.         WeatherButton.GetComponentInChildren<Text>().text = UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().weatherForecaster.ToString ();
    46.  
    47.  
    48.  
    49.     }
    50.  
    51. }
    As you can see, I declare the "weather" variable as an int corresponding to the one in the separate script, which then goes into your enum, which should spit out text in the log (I added the six because that's the current weather number I'm using). However, it doesn't output anything. The bottom button tells me the int as a string in my GUI in real-time, and it's indeed six. But I'm not sure why the enum isn't taking that and using it. Any help? And thanks again.

    Edit: it looks like with this the "weather" from the Start function isn't interfacing with the "weather" in the enum. So the enum is arbitrarily making a random variable "weather," then making certain inputs give certain outcomes...even though there are no real inputs. How do I get it to call from the referenced script rather than arbitrarily make up variables? or am I going about it completely wrong?
     
    Last edited: May 31, 2015
  11. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Well, I wasn't able to get either one of those working, and I have no idea why, so after some experimentation I resorted to using an if statement in Update that references the int from the other script, which returns a certain string to the GUI on certain int values:

    I feel like this is horribly unoptimized because Update is called every frame, but I couldn't get it to work calling the int in the class itself and then defining it in Start, so this will have to do for now at least. Thanks for all the help, anyway.

    EDIT: Scratch that, I looked a little more into it and now I'm using a switch statement like Iarku suggested, though I'm still declaring the variable in Update so I still think it's unoptimized. But it will have to work.

    Code (csharp):
    1. int weather = UniStormWeatherScript.GetComponent<UniStormWeatherSystem_C>().weatherForecaster;
    2.  
    3.     switch(weather){
    4.         case 7:
    5.             WeatherButton.GetComponentInChildren<Text>().text = "Clear";
    6.             break;
    7.         case 8:
    8.             WeatherButton.GetComponentInChildren<Text>().text = "Clear";
    9.             break;
    10.         case 10:
    11.             WeatherButton.GetComponentInChildren<Text>().text = "Clear";
    12.             break;
    13.         }
    Also, this forum needs a proper "strikethrough" button+hotkey without resorting to BBCode.
     
    Last edited: Jun 2, 2015