Question about the OnHit delegate

Hello (as usual, please let me know if I get the terminology wrong here),

I was just looking back over this project, and I noticed that in ProjectileBase’s OnHit() delegate function, we check that OtherActor is valid. I was confused for a moment about how the function gets OtherActor, and then remembered that the following line passes in the arguments from the OnComponentHit function.

	ProjectileMesh->OnComponentHit.AddDynamic(this, &AProjectileBase::OnHit);

I can see where this is declared in the PrimitiveComponent.h, but I don’t understand where it’s defined(?) in the PrimitiveComponent.cpp file. That leaves me still wondering how OnHit() ultimately manages to get and check whether OtherActor is valid.

That is just adding to the delegate. When that actually happens it’ll pass that information along.

As an example you can create your own delegate that passes in an int.

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FExampleDelegate, const FString&, Name);

class AExample : public AActor
{
     GENERATED_BODY()
public:
    void Tick(float DeltaTime) override;
    FExampleDelegate Example;
};

void AExample::Tick(float DeltaTime)
{
    if (FMath::IsNearlyEqual(GetWorld()->GetTimeSeconds(), 5.f))
    {
        Example.Broadcast(GetName());
    }
}

Then anything that does AddDynamic on that would get the name of AExample they binded to in their function e.g.

// somewhere
SomeExample->Example.AddDynamic(this, &AMyActor::DelegateExample);

void AMyActor::DelegateExample(const FString& Name)
{
    UE_LOG(LogTemp, Warning, TEXT("%s"), *Name);
}

Would be getting the name of SomeExample after 5 seconds since the world began.

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

Privacy & Terms