C# Delegates in Unity! - Intermediate Scripting Tutorial

Поділитися
Вставка
  • Опубліковано 15 вер 2024

КОМЕНТАРІ • 59

  • @vladimirkliman7185
    @vladimirkliman7185 4 роки тому +129

    delegate = container for function , like variables for values .. That´s what I needed to hear

    • @StringOfMusic
      @StringOfMusic 3 роки тому

      Exactly what I needed to hear too! Very concise and clear. Keep making these type of videos!

    • @hairohukosu433
      @hairohukosu433 3 роки тому +1

      @Damien Kane oh hey the bot has a friend

    • @Bryan-bh7cy
      @Bryan-bh7cy 3 роки тому

      @computer brand many

    • @Bryan-bh7cy
      @Bryan-bh7cy 3 роки тому +2

      @computer brand like when you’re trying to propagate a signal ~

    • @iteratedofficial
      @iteratedofficial 3 роки тому +2

      @computer brand When you wanna call many functions at once

  • @TheEarlyAstrotype1024
    @TheEarlyAstrotype1024 Рік тому +10

    Clearest explanation of delegates ever!
    So basically,
    we can use delegates to call multiple functions in one call!

  • @teennetworkce
    @teennetworkce 4 роки тому +51

    not everyone has the gift to explain well.
    loud and clear explanation. the best for unity learners on yt. thanks!

  • @Thoanga
    @Thoanga 4 роки тому +9

    Studying C# and your explanation of delegates is the simplest I've come across. Thank you!

  • @SurreyMuso
    @SurreyMuso 9 місяців тому +1

    Top job. This explains delegates far more clearly than any other specialist C# tutorial that I've seen.

  • @titastotas1416
    @titastotas1416 2 роки тому +1

    Returning and watching these tutorials once I finaly understand the power and the use cases feels so great.

  • @ignatiusreilly8280
    @ignatiusreilly8280 3 роки тому +6

    Crystal clear explanation. I was having trouble understanding the purpose of these.

  • @matthewbrown995
    @matthewbrown995 5 років тому +18

    I've never used a delegate before and this explained it very well to me. Thanks!

    • @batuhanartan
      @batuhanartan 4 роки тому +6

      Matthew, can you explain to me that why i should use delegate to call a function instead of calling function directly. What is the benefit of this? I just didn' t understand what are the advantages of delegates. Especially when calling one funciton.

    • @gamepass505
      @gamepass505 4 роки тому +4

      @@batuhanartan some times you want just a single function run into your scene while others are stopped , maybe you want to update your UI , but you have for example 3 functions that update an element of your UI ! so if you want to each time just call one of 3 functions , with out one disturb another you simply use delegate! and too many other usages for delegates!

    • @5rjoud
      @5rjoud 4 роки тому

      I've never used a delegate before and this explained very well why I didn't. Thanks!

    • @adriandeveraaa
      @adriandeveraaa 4 роки тому +2

      ​@@batuhanartan To answer you question it helps make your code cleaner. Heres the longer answer: Typically you have user input in your update method. We can do something simple where on Update we get the mouse position x,y and then do some calculation such that mouseCalculate(posx,posy); which is simple in itself. Later in the development if I wanted to do more complicated operations with mouseCalculate my update ends up having nested if statements. Delegates, like methods, can help reorganize your code and promote cleanliness.

    • @adriandeveraaa
      @adriandeveraaa 4 роки тому +1

      @@MarioTGP Yes and no. It helps seperate your code. Its also bad to have a long update function. Normally I separate my code in 2 ways: events and constant updates. From there I break it down in methods.

  • @lesarch
    @lesarch 3 роки тому

    This is the simplest explanation I've ever seen anywhere and I immediately understood how delegates worked. I've been spacing out so bad trying to figure this out myself since I couldn't wrap my head around the concept. This video easily made me understand the whole concept. Thank you!

  • @wowgingermobile2123
    @wowgingermobile2123 3 роки тому +2

    Delegates clarity 100% achieved! Thank you so much!

  • @gunasekhar9029
    @gunasekhar9029 2 роки тому

    This is how a tutorial should be. Now i can remember delegates for a long time

  • @ishanrevankar6736
    @ishanrevankar6736 3 роки тому

    what a clear and precise explanation, chefs kiss

  • @fulgencejuniorlohore854
    @fulgencejuniorlohore854 2 роки тому

    Thanks for your explanation. The concept of "delegates" is now clear for me.

  • @gregoryfenn1462
    @gregoryfenn1462 5 років тому +6

    I think delegates are super useful for events and event-oriented programming in general. You could also use them to make an array of functions to use (eg in some kind of custom frame scheduler, if you needed some functions to run in a very specific order).

  • @foreducation408
    @foreducation408 2 роки тому

    this lecture is soo good, maybe the best lecture on youtube about Delegates.

  • @changbroot
    @changbroot 4 роки тому +6

    I understand how to use it, but I don't get its use case. We could have just put PrintNum(50) in the start instead of the 4 lines of code that dose that. It would have been nice if they had some examples where this kind of functionality is needed.

  • @ksepastremenos
    @ksepastremenos 3 роки тому +2

    I bet this will help me understand C# events, which made no sense before

  • @themediocre_coder
    @themediocre_coder 3 роки тому

    This was a great explanation.. clear and concise thanks a lot!

  • @fahimfaysal1753
    @fahimfaysal1753 4 роки тому +2

    When will we have serializable delegates in the inspector so that we can stop relying on slow UnityEvents? Delegates are wonderful but due to lack of proper support, they do not mesh well with the unity workflow.

  • @kaleb_t4818
    @kaleb_t4818 3 роки тому +9

    i cant think of a good use case for this , can someone give me an example.

    • @Swyse
      @Swyse 3 роки тому +12

      If you have an event, for example a player dies, you might want a couple of stuff to happen from independent game objects. For example, the UI to display a "You died" text, other players' UI to display that you died in the corner of the screen, the world to spawn a lightning.
      So what you can do is create a delegate (e.g OnPlayerDied) on your character, and in your Die method, you call this delegate (OnPlayerDied() or OnPlayerDied.Invoke())
      The world can subscribe to this delegate, as well as the other players which means when you die, you broadcast this OnPlayerDied event to every object that was subscribed and they can act accordingly.

    • @kaleb_t4818
      @kaleb_t4818 3 роки тому

      @@Swyse oh so events are delegates , tnx

    • @moddingdudes7055
      @moddingdudes7055 3 роки тому

      If you’re using UNet or Mirror for multiplayer games and you want to use a sync dictionary, the hook is the callback which uses delegates so you can do stuff when the variable is changed

    • @IDontReadReplies42069
      @IDontReadReplies42069 2 роки тому +1

      ever use linq? Where(x => x.name) that thing inside is a lamda, which is an anonymous function which uses a delegate. Delegates are used to store methods as arguments.

  • @dreamether7808
    @dreamether7808 5 років тому +8

    very cool

  • @ansqad226
    @ansqad226 Рік тому +1

    1) how to call a delegate in another script?
    2) what does invoking an delegate mean?
    Please answer

  • @RetroBoxesShop
    @RetroBoxesShop Рік тому

    as i understood the delegate is like function call other function example:
    void fun1(){
    int a=1;
    fun2();
    }
    void fun2(){
    int b =2;
    }
    is this right?

  • @yawnyawning
    @yawnyawning 4 роки тому

    The best tutorial of Delegates ...

  • @valorantworld8895
    @valorantworld8895 3 роки тому

    10/10 teacher

  • @shuming6440
    @shuming6440 5 років тому +3

    Simple and clear ! 👍👍👍

  • @Ironlionm4n
    @Ironlionm4n Рік тому

    What about invoking delegated in a separate class?

  • @flyingjudgement
    @flyingjudgement 2 роки тому

    This is soo powerfull ! How performant heavy is this? Is it viable to assemble hundreds of objects from different parts this way?

  • @thatsalot3577
    @thatsalot3577 3 роки тому

    2:36 i love how the mesh filter name is just lemon_watermelon boi😂

  • @brandonfarfan1978
    @brandonfarfan1978 3 роки тому

    Good lesson. Thanks.

  • @cindyj8377
    @cindyj8377 5 років тому

    isnt the first delegatescript also doing a multicast cos both method is assigned to the same variable? or is it different because it uses only '=' and the multicast script uses '+='?

    • @Chubzdoomer
      @Chubzdoomer 4 роки тому +3

      Nope. Multicasting means assigning more than one method to a delegate.
      In the first script, the delegate is only ever assigned one method at a time (using =). This is _not_ multicasting.
      In the second script, however, the delegate is assigned _two_ methods (using +=) before it's called, which ultimately has the effect of two method calls with just a single line of code. This is multicasting in action!

    • @cindyj8377
      @cindyj8377 4 роки тому

      @@Chubzdoomer ah! i see now, thank you for the explanation! :)

    • @IDontReadReplies42069
      @IDontReadReplies42069 4 роки тому

      @@Chubzdoomer WHAAAT CHUBZ fancy seeing you here. Will we get any more ACS videos?

    • @Chubzdoomer
      @Chubzdoomer 4 роки тому

      @@IDontReadReplies42069 Yeah, I'll be doing more ACS tutorials for sure!

    • @IDontReadReplies42069
      @IDontReadReplies42069 4 роки тому

      @@Chubzdoomer awesome looking forward to it. I also have to ask, what brings you here? Do you do some work in unity?

  • @amac333
    @amac333 3 роки тому

    What is single argument?

  • @Mballs2478
    @Mballs2478 4 місяці тому

    Thanks Unity!