Dynamic Delegate Confusion

Hi,
I am having trouble understanding dynamic delegates.
I understand that it should be seen as an event instigator. Meaning, if something happens, like actor being hit, then call a function.

I am however having trouble in how these functions are created. More specifically their input parameters, in the HealthComponent and ProjectileBase classes. Where can I find documentation that will show the required input parameters for the “OnHit” function and “TakeDamage” functions?

Both of these have a long list of input parameters, however I do not know where these are derived from, is it suppose to be common knowledge?
Also, how are these communicating to each other? How can the OnHit Function, transfer information to a different class (HealthComponent) and then provide the 'TakeDamage" function with all its required input parameters to be able to cause damage?

Thanks in advance for any assistance.
Kind Regards
Tiaan Adlem

  1. Unfortunately these aren’t really documented as far as I know. If you go to definition for OnTakeAnyDamage you will get to
    /** Called when the actor is damaged in any way. */
    UPROPERTY(BlueprintAssignable, Category="Game|Damage")
    FTakeAnyDamageSignature OnTakeAnyDamage;
    
    If you then go to definition of FTakeAnyDamageSignature you will get this beauty
    DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_FiveParams( FTakeAnyDamageSignature, AActor, OnTakeAnyDamage, AActor*, DamagedActor, float, Damage, const class UDamageType*, DamageType, class AController*, InstigatedBy, AActor*, DamageCauser );
    
    The first three parameters are the name of the delegate, the class it’s on and the name of the variable. The rest are the parameters, the types and names of the parameters are separated by commas. Meaning the end bit
    AActor*, DamagedActor, float, Damage, const class UDamageType*, DamageType, class AController*, InstigatedBy, AActor*, DamageCauser );
    
    Should remove the commas between the types and parameters and there’s your function signature i.e.
    (AActor* DamagedActor, float Damage, const UDamageType* DamageType,  AController* InstigatedBy, AActor* DamageCauser );
    
    The class is acting as a forward declaration which you can remove.
  2. Because the delegate is holding all bound functions on it. When the event happens it would be doing something like (made up for demonstrative purposes)
    for (Functions Func : OnTakeAnyDamage)
    {
        Func(Arg1, Arg2, Arg3, ...);
    }
    

Thanks DanM,
Once again you have helped me greatly. Truly appreciate it. :smile:

Hi Dan,

Just to ensure I can find this definition in the future.
I am currently on lecture 152 of the C++ Unreal Engine Course. It is the ToonTanks game.
The OnHit and TakeDamage are dynamic delegate functions, and declared in the header files as follows:
HealthComponent.h


and
ProjectileBase.h

When I go to the definitions, it just takes me to the header file and the .cpp file. I am not being directed to the locations you screenshotted. To me it seems like the lecturer made these dynamic functions with unique names, so I do not know where to search for their input parameters if I wanted to one day do this in my project?

You’re looking at the functions you wanted to add to the delegate.
I’m saying you should go to definition on this

GetOwner()->OnTakeAnyDamage.AddDynamic(this, &UHealthComponent::TakeDamage);
//          ^-------------^

Thanks Dan,

This makes more sense now.
Appreciate the help.

Kind Regards
Tiaan Adlem

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.