Search Unity

Passing properties as arguments such that the resulting class can modify the original

Discussion in 'Scripting' started by ChickenVegetable, Apr 22, 2021.

  1. ChickenVegetable

    ChickenVegetable

    Joined:
    May 3, 2020
    Posts:
    87
    Hi

    I'm really not experienced with c# so maybe my whole methodology is wrong but here goes.

    I'm trying to build a GUI that allows many many parameters to be modified in real time and have a central repository for all those variables. So I am trying to build a wrapper class around the slider so I don't have to explicitly link it to each property in the parameters class manually, and make it easier to add other generic functionalities like events or whatever.

    So right now I have a UI class that holds the sliders, a slider wrapper class that holds some extra functionality, and a central parameters class. The central class shouldn't have any dependencies of the other two classes.

    The UI class instantiates the slider wrapper class, and passes a Unity slider object. That part works fine.
    It also passes the property from the central parameter class to which it should be linked. That's the part I'm stuck on.

    UI class:
    Code (CSharp):
    1. public class UIParams : MonoBehaviour
    2. {
    3.  
    4.     private GeneralParams _centralParams;   // reference to central parameters class
    5.     private Transform _mainUI;               // reference to main UI panel
    6.     private Transform _panel1;               // reference to sub panel
    7.  
    8.     private MySlider _mySlider1;           // instance of my slider wrapper class
    9.     // more sliders go here
    10.    
    11.     void Start()
    12.    
    13.     {
    14.         //init
    15.         _centralParams = GameObject.FindObjectOfType<GeneralParams>();
    16.         var Panel1 = GameObject.Find("MainUI").transform.Find("Panel1");
    17.  
    18.        
    19.        
    20.         //sliders
    21.         _mySlider1 = new MySlider(
    22.             Panel1.Find("Slider1").GetComponent<Slider>(),  //pass the actual slider to the wrapper
    23.             _centralParams.Parameter_1);                     //pass the central parameters property to the wrapper
    24.        
    25.        
    26.         // more sliders go here
    27.    
    28.     }
    29.  
    30. }
    Slider wrapper class:

    Code (CSharp):
    1. class MySlider
    2. {
    3.     private Slider _slider;                 // object to hold the slider
    4.     private float _sliderValue;                   // value of the slider
    5.  
    6.     public MySlider(Slider Slider, float CentralParameterProperty)
    7.     {
    8.         _slider = Slider;                                                   // reference to Slider
    9.         _slider.onValueChanged.AddListener(delegate { update();});      // listen for changes to slider, don't want to be checking all the time
    10.         _sliderValue = CentralParameterProperty;                            // set slider to default value in CentralParams. How do I modify that value???
    11.     }
    12.  
    13.     private void update()
    14.     {
    15.         _sliderValue = _slider.value;       // how do I change the original value from Central Params without explicitly linking each property manually?
    16.     }
    17. }
    Central parameter class:

    Code (CSharp):
    1. public class CentralParams : MonoBehaviour
    2. {
    3.    
    4.     public Action GeneralUpdateEvent;       // tell other classes when an update has occured
    5.    
    6.     [SerializeField]
    7.     private float parameter_1;
    8.     public float Parameter_1
    9.     {
    10.         get => parameter_1;
    11.         set
    12.         {
    13.             parameter_1 = value;
    14.             GeneralUpdate();
    15.         }
    16.     }
    17.    
    18.     // more parameters go here
    19.    
    20.  
    21.     void GeneralUpdate()
    22.     {
    23.         GeneralUpdateEvent?.Invoke();
    24.     }
    25. }
    Passing the float CentralParameterProperty into the MySlider instance works to set the default value of the slider. But obviously from that point onwards the slider only modifies the internal variable _sliderValue, not the property of the central class.

    Can I set it up so I pass some object from the UI class into the MySlider on instantiation such that the MySlider instance can both get and set the values of the CentralParams property, without explicitly linking to central class within the MySlider class? Or is this just Wong?

    Hope that made sense...
     
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Have you tried passing the parameter as a ref?
     
  3. ChickenVegetable

    ChickenVegetable

    Joined:
    May 3, 2020
    Posts:
    87
    Doesn't like that as the parameter is a property, trying to work out a way around that rn