Subscribing to AActor::OnEndOfPlay from another class

I’m trying to subscribe to OnEndOfPlay from a different actor class than the object that fires the event. Is this possible?

I’ll provide some context about what I’m trying to achieve with this:

I have spawn volumes with a limit of N enemies spawned per spawn volume instance. What I want is to subscribe to OnEndOfPlay of the recently spawned actor so the spawn volume can keep track of when an enemy dies and can resume respawning.

Edit: I figured the issue is mainly that attempting to bind my delegate to EndPlay instead of OnEndPlay, however I can’t figure out the syntax for declaring a compatible delegate to pass into the Add function, can anyone help me with that? Unreal engine sure needs more c++ code samples.

I already implemented the event in my enemy class, I’m having a hard time with the subscription from outside the enemy class in my case the spawn volume class.

Here’s what I have:

I override the event in my enemy class, in Enemy.h:

virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

And in Enemy.cpp

void AEnemy::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
    Super::EndPlay(EndPlayReason);
    UE_LOG(LogTemp,Log,TEXT("This is a message to yourself during runtime!"));
}

I define and implement the function to bind to the event like so:
In SpawnVolume.h:

void ActorDestoyed(const EEndPlayReason::Type EndPlayReason);

In SpawnVolume.cpp:

void ASpawnVolume::ActorDestoyed(const EEndPlayReason::Type EndPlayReason)
{

}

I bind the function the the event when I spawn enemies:

FActorSpawnParameters spawnParams;
            spawnParams.Owner = this;
            spawnParams.Instigator = Instigator;

            ACharacter* spawnedCharacter = World->SpawnActor<ACharacter>(WhatToSpawn, GetRandomPointInVolume(), this->GetActorRotation(), spawnParams);
            spawnedCharacter->SpawnDefaultController();
            
            AEnemy* enemy = Cast<AEnemy>(spawnedCharacter);
            
            if(enemy != nullptr)
            {
                enemy->EndPlay.AddDynamic(this, &ASpawnVolume::ActorDestoyed);
            }
            
            spawnCount ++;

This gives me the following compiler error:

reference to non-static member function must be called
Info                 enemy->EndPlay.AddDynamic(this, &ASpawnVolume::ActorDestoyed);
Info                 ~~~~~~~^~~~~~~

I haven’t used much c++ in over a decade so I’m slowly picking it up in unreal so it might be something stupid, any help?

EndPlay is not delegate it’s just virtual function which you can override, overall C++ don’t support natively delegates it’s a feature of UE4.

There is OnEndPlay delegate in actor you should use it insted:

So insted of EndPlay.AddDynamic() use OnEndPlay.AddDynamic() ;]

You can also invert call order , make enemy call out ASpawnVolume that it’s been destroyed.

If you want to look up what function you need to make, go to UE4 github and search for Delegate type which in this case is FActorEndPlaySignature and look for delegate declerartion that looks like this:

DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorEndPlaySignature, AActor*, Actor , EEndPlayReason::Type, EndPlayReason);

And oyu can clearly see here that you need ot make function with AActor* and EEndPlayReason::Type ;]

Thanks Shadowriver, you are right I’m totally calling the wrong thing here, however I can’t use AddDynamic to subscribe to OnEndPlay, it won’t compile, so I have to use Add which takes in a delegate who’s syntax declaration I can’t figure out, can you help me out with this?