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

Delegates used to replace multiple methods

Discussion in 'Scripting' started by codeBeast, Jul 6, 2021.

  1. codeBeast

    codeBeast

    Joined:
    Jun 8, 2013
    Posts:
    28
    Hi guys. I was watching a you tube video on delegates and their uses. I thought I understood them perfectly but it seems in this case I don't. Every single tutorial I have seen assigns the delegate to a function or rather the function/method subscribes to the delegate. However, in this case I can't see where the coder has assigned the delegate. ie: ScoreDelegate has the same signature and parameter as ScoreByKillCount and ScoreByFlagCaptures but I can't see how the latter two "know about" the delegate as they have not been assigned. I mean usually you would see smth like
    ScoreByKillCount = ScoreDelegate
    Any help would be appreciated.

    upload_2021-7-6_11-56-56.png
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You don't need to assign them to anything, delegates only tell the compiler, that wherever they appear, they expect a method of that signature. As long as that signature matches, you have a method that can be used where that delegate is used.
     
    codeBeast likes this.
  3. codeBeast

    codeBeast

    Joined:
    Jun 8, 2013
    Posts:
    28
    So where exactly is that happening in the code because I can see ScoreCalculator used as a parameter in GetPlayerNameToScore but I can't see how ScoreByKillCount and ScoreByFlagCaptures are related in any way.
    A delegate usually holds a reference to a function but I can't see that relationship anywhere. Sorry if I'm a bit slow.
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    GetPlayerNameTopScore function has the delegate instance that holds the reference to the function as its second parameter. You pass in the function you want it to run when it calls
    scoreCalculator(stats)
    .

    delegate int ScoreDelegate(PlayerStats stats);
    just defines a delegate shape. It doesn't do anything without an instance, just like a class.

    ScoreDelegate myDelegateInstance;
    is an example of an instance. It can store any function that fits the signature.

    myDelegateInstance = ScoreByKillCount;
    stores the function.

    myDelegateInstance(...);
    calls ScoreByKillCount.



    There is a faster way to do this using Action/Func shorthand using the System namespace. It saves you having to define the ScoreDelegate shape.

    void GetPlayerNameTopScore(Func<PlayerStats, int> scoreCalculator)
    {
    ...
    }

    GetPlayerNameTopScore(ScoreByKillCount);[/code]




    The next step is writing your functions inline using anonymous functions.
    Code (csharp):
    1. var playerNameMostKills = GetPlayerNameTopScore(allPlayerStats, stats => stats.kills);
    2. var playerNameMostFlags = GetPlayerNameTopScore(allPlayerStats, stats => stats.flagsCaptured);
    Now you don't need the delegate definition, or the functions, or the variable type (using 'var').
     
    Last edited: Jul 6, 2021
    codeBeast likes this.
  5. codeBeast

    codeBeast

    Joined:
    Jun 8, 2013
    Posts:
    28
    Wow - what a beauty. I had read about lambdas making things nice and neat but I haven't got enough experience. Looks like the best way to go. Lovely. THANK YOU.

    So, was I right re: one thing? That there was no "relationship anywhere" between delegate and other functions.

    You said
    myDelegateInstance = ScoreByKillCount; stores the function.
    myDelegateInstance(...); calls ScoreByKillCount.

    But that code is nowhere to be seen in the code.
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,532
    There is no need to store the delegate in a variable. It may be passed directly to the method when it's called. That information is not in the code you posted. So where and how is your "GetPlayerNameTopScore" method called?

    The delegate is called inside your "GetPlayerNameTopScore" method in line 23. However what you haven't shown what is passed into the delegate when the method GetPlayerNameTopScore method is called.

    edit

    I just realised the method in question is called inside your OnGameOver method two times. The lines in question are line number 6 and 7. As you can see, in line 6 you pass the method ScoreByKillCount as delegate and in line 7 you pass the method ScoreByFlagCapture to the method.

    Note that in those lines you do not execute the methods "ScoreByKillCount" and "ScoreByFlagCapture" but you just pass the method itself as delegate to the "GetPlayerNameTopScore" method.
     
    codeBeast likes this.
  7. codeBeast

    codeBeast

    Joined:
    Jun 8, 2013
    Posts:
    28
    Well that's what I'm also trying to say - that is ALL the code in the tut - screen capture from the You Tube tutorial. It's not my code.
    Basically, I'm not seeing all "the relationships".
    What I have been saying from the beginning - where is the relationship of the "delegate to something".
    I take you pointer on the "The delegate is called inside your "GetPlayerNameTopScore" method in line 23" but here are still "gaps"in the logic. OR I'm a total newbie.
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    They're here:
    upload_2021-7-6_9-43-35.png

    They've passed the delegate references as parameters.

    The variable they are assigned to is the paremeter. So in GetPlayerNameTopScore, they are assigned to 'scoreCalculator'.

    Lets pretend GetPlayerNameTopScore took an int as the 2nd parameter... you could say:
    GetPlayerNameTopScore(allPlayerStats, 5);

    or if it took a component:
    GetPlayerNameTopScore(allPlayerStats, this.GetComponent<SomeComponent>());

    You don't have to necessarily assign it to a variable.
     
    codeBeast likes this.
  9. codeBeast

    codeBeast

    Joined:
    Jun 8, 2013
    Posts:
    28
    Brilliant answers guys. I feel a little stupid now - as "lordofduct" has pointed out exactly how and where the delegates + associated functions are used. I should have looked a little harder.
    The good things however, is that I have learnt even more now. "stardog" loved your post.