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

Why use getters and setters?

Discussion in 'Scripting' started by GigiB, Sep 7, 2014.

  1. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    Hey so I;m watching this tutorial:

    and it's really good but I notice he uses getters ans setters several time. My question is why? Is this a way to access a function from another script or.

    I'm kind of lost all the documentation I look up gives me the professional answer that makes my head hurt.
     
  2. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    You can use a setter to trigger other functionality. For example, I have a static class which holds the name of a level to load. The getter is a plain return value, but the setter sets the internal variable and loads the loading screen.

    There's also the option to add validation in the setter. You could simply not change the variable if it doesn't match certain criteria. It's a nice place to put a few Debug.Log() calls when you're wondering what's going on too :)
     
    NomadKing likes this.
  3. vladimirdlc

    vladimirdlc

    Joined:
    Jan 26, 2013
    Posts:
    19
    The advantage of using private variables with getters and setters, can range from validation, to testing, to security to just keeping coding standards and encapsulation.

    If you keep your variables only accessible trough getters, you can control exactly what you want to return, if it isn't just the raw data, you can add and do anything in those methods before returning the values.
     
  4. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    Ok so your saying for example in the tutorial he made an overloaded version of many of his functions and the getter and setter where to return a value for the functions and to "set" a value. In order to understand this do you think I need to research overloaded funtions.
     
  5. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    Those are independent things. Overloading is a separate subject :)
     
  6. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    So you would use "getters and setters" versus making your variables static? Please tell me if I'm wrong, so getter and setter help other classes get data from a method but not access it (since it's private).

    So if I needed a "taxation function" to check the time of day before it fires (and taxed my citizens). I would make a private function to calculate time of day and have getters and setters added to it ? I'm sorry if I completely missed the mark.
     
  7. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    Ok so I think I'm starting to get it but why does he use them in the video?

    I'm really sorry if I seem dense.
     
  8. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    If it's part of a series, I'm sure there is purpose that is made clear later.

    Overloading is for specifying methods that derived classes use. And THAT is something you can read more about on Microsoft's C# site :)
     
  9. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    Ok thank you btw!
     
  10. AndyLL

    AndyLL

    Joined:
    Aug 25, 2013
    Posts:
    75
    It seems we are talking apples and oranges when we are talking about getters/setters and static variables.

    The 2 are not inter-changeable.

    You use getters/setters in place of public variables.

    You do it to abstract out the meaning of the variable from the storage of the variable and to control access to it.

    For example... lets say you have a class that sets/returns the your money balance

    Code (CSharp):
    1.  
    2. // public way
    3. public class myBalanceClass
    4. {
    5.       public double Balance;
    6. }
    7.  
    and you create the class:

    Code (CSharp):
    1.  
    2. myBalanceClass mybalance = new myBalanceClass ();
    3.  
    You might have 100s of places setting/getting the time this way:

    Code (CSharp):
    1.  
    2. double currentbalance = = mybalance->Balance;
    3. mybalance->balance =100.00;
    4.  

    However... if you use accessors

    Code (CSharp):
    1.  
    2. // same class using accessors
    3. public class myBalanceClass
    4. {
    5.       private double _balance;
    6.       public double
    7.          {
    8.          get { return _balance;}
    9.          set { _balance= value;}
    10.           }
    11. }
    12.  
    You are still access it the same way in your application:

    Code (CSharp):
    1.  
    2. double currentbalance = = mybalance->Balance;
    3. mybalance->balance =100.00;
    4.  
    However... now you have gone international. Your balances are stored in USA dollars but you have German users that want to see it in Euros

    Using accessors... all you have to change is the class

    Code (CSharp):
    1.  
    2. public class myBalanceClass
    3. {
    4.       private double _balance;
    5.       public double
    6.          {
    7.          get { return _balance * GetUsersExchangeRate();}
    8.          set { _balance= value / GetUsersExchangeRate();}
    9.           }
    10. }
    11.  
    Nothing has to change anywhere else but now you are seeing the balance in the users currency and they are updating it in their currency.

    You can also use getters/setters for validation. Say you never want the balance to go below 0.

    Using a public variable nothing stops this statement:

    Code (CSharp):
    1.  
    2. mybalance->balance = -99.99;
    3.  
    However... using getter/setters

    Code (CSharp):
    1.  
    2. public class myBalanceClass
    3. {
    4.       private double _balance;
    5.       public double
    6.          {
    7.          get { return _balance * GetUsersExchangeRate();}
    8.          set
    9.          {
    10.          if (value > 0)
    11.             _balance= value / GetUsersExchangeRate();}
    12.          else
    13.             _balance = 0.0;
    14.             {
    15.           }
    16. }
    17.  
     
    Last edited: Sep 9, 2014
    NamelessGames_ and jorgekaramba like this.
  11. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    wow thank you that's a very good explanation.
     
  12. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    Thank you all for helping me out