Hey! After watching the video a few times and searching the web for some time, I still couldn’t grasp the concept of delegates. Can someone explain what it means? Thanks.
When you use AddDynamic you are telling it that when that event happens (e.g. OnHit, OnTakeAnyDamage), to call your member function with the instance you gave it.
ProjectileMesh->OnComponentHit.AddDynamic(this, &ProjectileBase::OnHit);
So the member function is ProjectileBase, and the instance is ProjectileMesh right?
I also get what the function does, but I don’t understand what delegate means. Can you explain it to me in a simple way?
No, AProjectileBase
is a type not a member function.
ProjectileMesh->OnComponentHit.AddDynamic(this, &AProjectileBase::OnHit);
ProjectileMesh
- Instance that has the delegate.
OnComponentHit
- The delegate that we wish to add to.
AddDynamic
- A helper macro for calling __InternalAddDynamic
which adds to the list of functions the delegate will call.
this
- The (pointer to the) instance we want the delegate to call the function on.
&AProjectileBase::OnHit
- The address of the member function.
If you understand what it does you understand what a delegate is as that’s what a delegate is.
Ok, so it is basically just a function that gets called when an event happens?
OnComponentHit is the delegate. It will call the added functions when the event happens.
Maybe a code example will help (note, not actual implementation Unreal uses)
Demo: Compiler Explorer
Ok, I think I get the concept now. Many thanks!
Hey there,
I’ve also been reading over this topic.
I started a thread earlier asking about certain functions, but now I realise that it wasn’t just the functions that were causing me problems (which DanM managed to solve), but it was the interaction of those functions with “Dynamic Delegates”.
Here are a couple of other resources I found.
What does delegate mean?
In this context, delegate mean to on-pass an action.
Something happens, then we delegate or hand-off action to some other function instead.
The way I see it, is that we have TWO functions.
We want to trigger the SECOND function when the FIRST function is fired off.
First: OnComponentHit()
.
Like the name suggests, this function is automagically sprung into action whenever the xxxx component is hit by something.
What now?
We want to fire off another function when OnComponentHit
registers a hit.
We use AddDynamic.
That will trigger the second function as the first function is fired.
What function will be fired? The one that we specify here: &AProjectileBase::OnHit);
Sometimes another way of describing the same issue helps.
That’s what I found with the two articles I linked.
Thanks! This help me clear my remaining questions about delegates. Definitely recommend others to take a look.
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.