Search Unity

delegates

Discussion in 'Scripting' started by Bolt, Oct 19, 2017.

  1. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    the delegates have not yet understood what they can serve, I read about books, websites but I do not know how to use them properly in games. Do you have any examples?
     
  2. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    It depends on what you want to do.
    Delegates are just a structure for functions and methods.
    For example, you can store a method in a variable and call it using the delegate structure.

    Hope this helps.
    Thanks.
     
  3. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    151/5000
    Sincerely, I'm not clear yet. I'm used to using methods and variables normally. I have never used delegates and events in the games field
     
  4. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    Events and Delegates are useful.

    For example you can use a delegate to specify what a back button should do:

    Code (CSharp):
    1. Action backButtonDelegate;
    2.  
    3. if (gameIsRunning) {
    4.     backButtonDelegate = PauseGame();
    5. } else if (isPaused) {
    6.     backButtonDelegate = ResumeGame();
    7. }
    8.  
    9. Button myBackButton = GetBackButton();
    10. myBackButton.onClick.AddListener(backButtonDelegate);
    Here are some useful docs:
    Hope this helps.
    Thanks.
     
    gjf likes this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    It is confusing. Microsoft made up the name Delegate just for C# - the name everyone else uses is "function pointers" or just "variables that can stand for functions." Microsoft also uses the word delegate in three different, similar ways AND there's a "design pattern" named delegate. So it's a hard word to look up and get consistent answers.

    I haven't seen any of the links above. Well some. But a text-hooky explanation is at http://www.taxesforcatses.com/TOC.shtml and go down to the 2nd part where it says "function pointers" (it's a PDF.)
     
  6. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Simplest example I can come up with on how to use delegates:

    Code (csharp):
    1.  
    2.  
    3. public class PlayerStats
    4. {
    5.   // Create an event that contains no delegate to avoid null checking before calling it
    6.   public event Action<int> OnHealthChanged = delegate{};
    7.  
    8.   private int _health;
    9.   public int Health
    10.   {
    11.     get { return _health; }
    12.     set { _health = value; OnHealthChanged(_health); }
    13. }
    14.  
    15. public class HealthBar : MonoBehaviour
    16. {
    17.   private PlayerStats _stats;
    18.   private Slider _slider;
    19.  
    20.   private void Start()
    21.   {
    22.     _slider = GetComponent<Slider>();
    23.     _stats = //.... assign it somehow - maybe find a Player gameobject and get stats from it
    24.  
    25.     _stats.OnHealthChanged += PlayerHealthChanged;
    26.   }
    27.  
    28.   private void PlayerHealthChanged(int newValue)
    29.   {
    30.     _slider.value = newValue;
    31.   }
    32. }
    33.  
    Disclaimer: May or may not compile as I typed this by hand. You should get the idea, though.

    Action/Func are a much nicer way to handle delegates rather than using the (IMO) clumsy delegate keyword.

    But yes, as said, delegates are just another way to say function pointers. Create events, or pass as a parameter to functions to use as a callback method.