I was trying to read some source code of UE4, specifically Actor.h, and I saw the variable:
UPROPERTY(BlueprintAssignable, Category=“Collision”)
FActorBeginOverlapSignature OnActorBeginOverlap;
I though, oh, this is what that OnActorBeginOverlap.AddDynamic comes from, great, let’s see how it is triggered, so I look for what is an FActorBeginOverlapSignature, and I got to:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorBeginOverlapSignature, AActor*, OverlappedActor, AActor*, OtherActor );
at the same head file Actor.h, and as far as I can go, this is the origin of FActorBeginOverlapSignature, I am like what the heck, if this is the origin of FActorBeginOverlapSignature, then how does the engine know to call OnActorBeginOverlap, when two Actors are overlapping? is there any hidden connection between when two actor are overlapping and FActorBeginOverlapSignature?
I am so confused.
The engine calls these delegates when needed. Delegates are basically function pointers to a specific method in a class, dressed up in a macro and passed a class pointer. The idea of Unreal Engine is to abstract a lot of these details away from you, but what you’d find is that it would call the delegate in the base AActor class and these would propagate down to your derived classes.
Hope this helps!
Thank you! I think I understand it now.
I did more digging, I think deleagate is used to call a member function of a class from another class without having to make two classes knowing(#include) eachother, to make it work, first, declare a delegate in the header file of a class, for example, your AMyGameMode class(could be name differently)
//a delegate calls a function with one paramter of type float
DECLARE_DELEGATE_OneParam(FCustomDelegateSignature, float)
Then, in the AMyGameMode class declaration, create a member variable of this delegate:
FCustomDelegateSignature CustomDelegate;
In anoter class B, define a fuction with one float paramater:
void FunctionWillBeCalledByDelegate(float paramater);
somewhere in class B, mosty maybe in the constructor, you can get a reference of AMyGameMode , and bind the funcion FunctionWillBeCalledByDelegate with the delegate CustomDelegate of AMyGameMode like this:
AMyGameMode* currentGameMode = (AMyGameMode*)GetWorld()->GetAuthGameMode();
CurrentGameMode->CustomDelegate.BindUObject(this, &ClassB::FunctionWillBeCalledByDelegate, 100.0f);
Somewhere in the implementation of class A,if you call:
CustomDelegate.ExecuteIfBound();
then, FunctionWillBeCalledByDelegate(100) will be called.
Oh, the last step is actually:
Somewhere in the implementation of AMyGameMode,if you call: CustomDelegate.ExecuteIfBound();
then, FunctionWillBeCalledByDelegate(100) will be called.