From c824f77e6da317cffcee3a461e62aaba2a7ef154 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Mon, 22 Oct 2018 15:01:24 +0200 Subject: [PATCH] Destroy pawn's controller when destroying an actor if necessary --- .../Source/Carla/Actor/ActorDispatcher.cpp | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorDispatcher.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorDispatcher.cpp index dd4acde7f..6358904ef 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorDispatcher.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorDispatcher.cpp @@ -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(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; }