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.
@@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!
@@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.
@@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.
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!
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).
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.
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.
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.
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
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.
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 '+='?
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!
delegate = container for function , like variables for values .. That´s what I needed to hear
Exactly what I needed to hear too! Very concise and clear. Keep making these type of videos!
@Damien Kane oh hey the bot has a friend
@computer brand many
@computer brand like when you’re trying to propagate a signal ~
@computer brand When you wanna call many functions at once
Clearest explanation of delegates ever!
So basically,
we can use delegates to call multiple functions in one call!
not everyone has the gift to explain well.
loud and clear explanation. the best for unity learners on yt. thanks!
Studying C# and your explanation of delegates is the simplest I've come across. Thank you!
Top job. This explains delegates far more clearly than any other specialist C# tutorial that I've seen.
Returning and watching these tutorials once I finaly understand the power and the use cases feels so great.
Crystal clear explanation. I was having trouble understanding the purpose of these.
I've never used a delegate before and this explained it very well to me. Thanks!
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.
@@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!
I've never used a delegate before and this explained very well why I didn't. Thanks!
@@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.
@@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.
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!
Delegates clarity 100% achieved! Thank you so much!
This is how a tutorial should be. Now i can remember delegates for a long time
what a clear and precise explanation, chefs kiss
Thanks for your explanation. The concept of "delegates" is now clear for me.
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).
this lecture is soo good, maybe the best lecture on youtube about Delegates.
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.
I bet this will help me understand C# events, which made no sense before
This was a great explanation.. clear and concise thanks a lot!
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.
i cant think of a good use case for this , can someone give me an example.
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.
@@Swyse oh so events are delegates , tnx
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
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.
very cool
1) how to call a delegate in another script?
2) what does invoking an delegate mean?
Please answer
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?
The best tutorial of Delegates ...
10/10 teacher
Simple and clear ! 👍👍👍
What about invoking delegated in a separate class?
This is soo powerfull ! How performant heavy is this? Is it viable to assemble hundreds of objects from different parts this way?
2:36 i love how the mesh filter name is just lemon_watermelon boi😂
Good lesson. Thanks.
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 '+='?
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!
@@Chubzdoomer ah! i see now, thank you for the explanation! :)
@@Chubzdoomer WHAAAT CHUBZ fancy seeing you here. Will we get any more ACS videos?
@@IDontReadReplies42069 Yeah, I'll be doing more ACS tutorials for sure!
@@Chubzdoomer awesome looking forward to it. I also have to ask, what brings you here? Do you do some work in unity?
What is single argument?
Thanks Unity!