Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Auto change Value based on the Enum.

Discussion in 'Scripting' started by WanAmir, Apr 24, 2021.

  1. WanAmir

    WanAmir

    Joined:
    Jul 17, 2018
    Posts:
    27
    Hi everyone!
    I have this setup on my Customer game object:

    code1.png

    In the Spawner script, the Spawner instantiate Customer and set their currentSession.
    In the Customer script, the Customer then set their maxCalories based on the currentSession. For example, if the spawner set currentSession to Breakfast, the Customer set their maxCalories to breakfastCalories and so on.

    This is what I've done:
    code2.png

    This is working as intended but it's getting hard to manage if there's more currentSession. So how I automatically change the maxCalories based on the currentSession? Is there any other approaches beside this?

    Thank you in advance :)
     

    Attached Files:

    • 1.png
      1.png
      File size:
      11.1 KB
      Views:
      251
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You could enhance this by instead of having individual serialized fields for each session, doing something like this:
    Code (CSharp):
    1. [Serializable]
    2. public struct Calories {
    3.   public SessionType sessionType;
    4.   public float amount;
    5. }
    6.  
    7. // This array is the data you will configure in the inspector.
    8. public Calories[] calories;
    9. Dictionary<SessionType, float> calorieDict = new Dictionary<SessionType, float>();
    10.  
    11. void Awake() {
    12.   foreach (var c in calories) {
    13.     calorieDict[c.sessionType] = c.amount;
    14.   }
    15. }
    16.  
    17. void SetMaxCalories() {
    18.   maxCalories = calorieDict[currentSession];
    19. }
    Then you just need to configure everything in the inspector for your character and you're good to go.
     
    WanAmir likes this.
  3. WanAmir

    WanAmir

    Joined:
    Jul 17, 2018
    Posts:
    27
    Wow! It works flawlessly. I don't quite understand Dictionary and struct. I never use those before.
    A little explanation really appreciated!
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You can think of a struct as basically the same as a class for now. The difference is subtle (although very important), but in the current context, that could be a struct or a class and you wouldn't really notice a difference. If you want to read more about them you can do so here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct

    A dictionary is a data structure optimized for fast lookups and insertions of values based on a key. In our case, the key is the SessionType and the value is the amount. Unlike a list where you would have to check every element in the list to find the one you want, the Dictionary is sort of like an array, where if you know the index of the element you care about, you can jump right to that element. For an array the index is always an integer, but for a dictionary, the index (called a key) can be any type you want more or less. A Dictionary is C#'s version of the well-known Hash Table data structure: https://en.wikipedia.org/wiki/Hash_table