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?