Search Unity

Question Help with button script to make code more concise

Discussion in 'Scripting' started by Dragomordor, Jan 25, 2023.

  1. Dragomordor

    Dragomordor

    Joined:
    Jan 19, 2023
    Posts:
    3
    So I am new at unity and C#, and I am trying to create a button that updates variables I made from another script. I am using the button script as a generic template for a button where I can attach the script and only change the Modifiers for each stat I defined. Any help would be greatly appreciated.

    I want to know if it is possible to make this button script more concise than what I have done here below.


    The button script: (Which I want to make more concise if possible if I add more stats)

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class TestButtonScript : MonoBehaviour
    8. {
    9.     public Button TestButton;
    10.     public UI_Behaviour UiBehaviourScript;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         TestButton.onClick.AddListener(TaskOnClick);
    16.     }
    17.  
    18.     public void TaskOnClick()
    19.     {
    20.         // Modifiers for each stat
    21.         int HealthModifier      = -5;
    22.         int HeatModifier        = 0;
    23.         int RespectModifier     = 0;
    24.         int LuckModifier        = 0;
    25.         int CashModifier        = 0;
    26.  
    27.         // Modify Health
    28.         UiBehaviourScript.Health = UiBehaviourScript.ModifyStat(UiBehaviourScript.Health, HealthModifier, UiBehaviourScript.MaxHealth, UiBehaviourScript.MinHealth);
    29.         // Modify Heat
    30.         UiBehaviourScript.Heat = UiBehaviourScript.ModifyStat(UiBehaviourScript.Heat, HeatModifier, UiBehaviourScript.MaxHeat, UiBehaviourScript.MinHeat);
    31.         // Modify Respect
    32.         UiBehaviourScript.Respect = UiBehaviourScript.ModifyStat(UiBehaviourScript.Respect, RespectModifier,UiBehaviourScript.MaxRespect,UiBehaviourScript.MinRespect);
    33.         // Modify Luck
    34.         UiBehaviourScript.Luck = UiBehaviourScript.ModifyStat(UiBehaviourScript.Luck, LuckModifier, UiBehaviourScript.MaxLuck, UiBehaviourScript.MinLuck);
    35.         // Modify Cash
    36.         UiBehaviourScript.Cash = UiBehaviourScript.ModifyStat(UiBehaviourScript.Cash, CashModifier, UiBehaviourScript.MaxCash, UiBehaviourScript.MinCash);
    37.         // Update UI
    38.         UiBehaviourScript.UpdateUI();
    39.     }
    40. }
    41.  

    The ModifyStat() method from the first script is as follows:

    Code (CSharp):
    1.    
    2.     public int ModifyStat(int ModifiedStat,int StatModifier, int MaxModifiedStat, int MinModifiedStat)
    3.     {
    4.         ModifiedStat += StatModifier;
    5.  
    6.         if (ModifiedStat > MaxModifiedStat)
    7.             ModifiedStat = MaxModifiedStat;
    8.  
    9.         if (ModifiedStat < MinModifiedStat)
    10.             ModifiedStat = MinModifiedStat;
    11.  
    12.         return ModifiedStat;
    13.     }
    14.  

    How I defined the variables I am referencing in the button script from the first script:


    Code (CSharp):
    1.     // Main Stat Variables
    2.     [HideInInspector]
    3.     public int Health, Heat, Respect, Luck, Cash;
    4.     // Main Stat Min Variables
    5.     [HideInInspector]
    6.     public int MaxHealth, MaxHeat, MaxRespect, MaxLuck, MaxCash;
    7.     // Main Stat Max Variables
    8.     [HideInInspector]
    9.     public int MinHealth, MinHeat, MinRespect, MinLuck, MinCash;
    10.  
     
    Last edited: Jan 25, 2023
  2. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    it seems like you have a concept that i would call "a Stat". each stat has a value, a min value, and a max value. maybe have a:
    Code (CSharp):
    1. public class Stat{
    2.     int val, minVal, maxVal;
    3.  
    4.     public void Modify(int StatModifier)
    5.     {
    6.         val = Mathf.Clamp(val + StatModifier, minVal, maxVal);
    7.     }
    8. }
    this will save you from rewriting the logic more often than necesary and allow for more convenien scaleability of stats' behavior if necesary. For me, the real win is preventing yourself from making silly mistakes like using clamping your health value between minHeat and maxRespect by accident. the only place to mess that up is in the creation of the healthStat variable and after that, it knows how to handle itself.
     
    Last edited: Jan 25, 2023
  3. Dragomordor

    Dragomordor

    Joined:
    Jan 19, 2023
    Posts:
    3
    Thank you for the quick reply!
    Since I am learning unity, would you mind telling me if this is the correct approach for using this class:

    I could create a new stat/object called "Food" of the type Stat as follows:
    Code (CSharp):
    1. Stat Food = new Stat();
    And then I can modify it's value by doing the following, which will increase my Food stat by 5:
    Code (CSharp):
    1. Food.Modify(5)
     
  4. Dragomordor

    Dragomordor

    Joined:
    Jan 19, 2023
    Posts:
    3

    And if I wanted to set the inital values, minValues and maxValues (along with a name) I could define the class as follows:

    Code (CSharp):
    1. public class Stat{
    2.     int val, minVal, maxVal;
    3.     public string name;
    4.  
    5.     public Stat(string name, int val, int minVal, int maxVal)
    6.     {
    7.         this.name = name;
    8.         this.val = val;
    9.         this.minVal = minVal;
    10.         this.maxVal = maxVal;
    11.     }
    12.     public void Modify(int StatModifier)
    13.     {
    14.         val = Mathf.Clamp(val + StatModifier, minVal, maxVal);
    15.     }
    16. }
    Then I could create a new stat as follows:

    Code (CSharp):
    1. Stat Food = new Stat("Food", 100, 0, 200);
    where the intial value is 100, the minValue is 0 and the maxValue is 200
     
  5. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    yep, you get it. you may want to add logic to gaurantee that minValue <= maxValue. maybe also that your initial value is valid. I would use a function name of "Increment(int amount)" or something that you wont get confused about later on, but thats just taste, and even the other suggestions are just pedantic safety measures that i may advise to myself and possibly skip over. another note, is that field and variable names do not start with capital letters. This is just convention, not a rule. having conventions will help you quickly identify the role of everything in your code, but most importantly allows for smooth communication with team members and people online who may take a look at your code. you don't need to worry about this stuff too much right now, but you seem motivated to be great, so just giving some tips :)

    But everything you said before is correct. you and your code will be fine ignoring me here.