Destroy pawn's controller when destroying an actor if necessary

This commit is contained in:
nsubiron 2018-10-22 15:01:24 +02:00
parent 571f9363e1
commit c824f77e6d
1 changed files with 22 additions and 3 deletions

View File

@ -10,6 +10,8 @@
#include "Carla/Actor/ActorBlueprintFunctionLibrary.h"
#include "Carla/Actor/CarlaActorFactory.h"
#include "GameFramework/Controller.h"
void FActorDispatcher::Bind(FActorDefinition Definition, SpawnFunctionType Functor)
{
if (UActorBlueprintFunctionLibrary::CheckActorDefinition(Definition))
@ -74,18 +76,35 @@ bool FActorDispatcher::DestroyActor(AActor *Actor)
UE_LOG(LogCarla, Error, TEXT("Trying to destroy nullptr actor"));
return false;
}
// Check if the actor is in the registry.
auto View = Registry.Find(Actor);
if (!View.IsValid()) {
UE_LOG(LogCarla, Error, TEXT("Trying to destroy actor that is not in the registry"));
UE_LOG(LogCarla, Warning, TEXT("Trying to destroy actor that is not in the registry"));
return false;
}
const auto &Id = View.GetActorDescription()->Id;
UE_LOG(LogCarla, Log, TEXT("Destroying actor '%s'"), *Id);
// Destroy its controller if present.
auto Pawn = Cast<APawn>(Actor);
auto Controller = (Pawn != nullptr ? Pawn->GetController() : nullptr);
if (Controller != nullptr)
{
UE_LOG(LogCarla, Log, TEXT("Destroying actor's controller: '%s'"), *Id);
auto Success = Controller->Destroy();
if (!Success)
{
UE_LOG(LogCarla, Error, TEXT("Failed to destroy actor's controller: '%s'"), *Id);
}
}
// Destroy the actor.
UE_LOG(LogCarla, Log, TEXT("Destroying actor: '%s'"), *Id);
if (Actor->Destroy())
{
Registry.Deregister(Actor);
return true;
}
UE_LOG(LogCarla, Log, TEXT("Failed to destroy actor '%s'!"), *Id);
UE_LOG(LogCarla, Error, TEXT("Failed to destroy actor: '%s'"), *Id);
return false;
}