Fixed missed actor's pointers

This commit is contained in:
doterop 2020-03-16 15:19:21 +01:00 committed by bernat
parent ed9f2bda51
commit f34b884fc1
5 changed files with 103 additions and 18 deletions

View File

@ -55,7 +55,6 @@ static auto FWorldObserver_GetActorState(const FActorView &View, const FActorReg
else if (AType::Walker == View.GetActorType())
{
UE_LOG(LogCarla, Warning, TEXT(" FWorldObserver_GetActorState Walker"));
auto Walker = Cast<APawn>(View.GetActor());
auto Controller = Walker != nullptr ? Cast<AWalkerController>(Walker->GetController()) : nullptr;
if (Controller != nullptr)
@ -87,14 +86,24 @@ static auto FWorldObserver_GetActorState(const FActorView &View, const FActorReg
UTrafficLightController* Controller = TrafficLightComponent->GetController();
ATrafficLightGroup* Group = TrafficLightComponent->GetGroup();
state.traffic_light_data.state = static_cast<TLS>(TrafficLightComponent->GetLightState());
state.traffic_light_data.green_time = Controller->GetGreenTime();
state.traffic_light_data.yellow_time = Controller->GetYellowTime();
state.traffic_light_data.red_time = Controller->GetRedTime();
state.traffic_light_data.elapsed_time = Group->GetElapsedTime();
state.traffic_light_data.time_is_frozen = Group->IsFrozen();
// Nobody is using this right now, perhaps we should remove it?
state.traffic_light_data.pole_index = TrafficLight->GetPoleIndex();
if (!Controller)
{
UE_LOG(LogCarla, Error, TEXT("TrafficLightComponent doesn't have any Controller assigned"));
}
else if (!Group)
{
UE_LOG(LogCarla, Error, TEXT("TrafficLightComponent doesn't have any Group assigned"));
}
else
{
state.traffic_light_data.state = static_cast<TLS>(TrafficLightComponent->GetLightState());
state.traffic_light_data.green_time = Controller->GetGreenTime();
state.traffic_light_data.yellow_time = Controller->GetYellowTime();
state.traffic_light_data.red_time = Controller->GetRedTime();
state.traffic_light_data.elapsed_time = Group->GetElapsedTime();
state.traffic_light_data.time_is_frozen = Group->IsFrozen();
state.traffic_light_data.pole_index = TrafficLight->GetPoleIndex();
}
}
}
}

View File

@ -34,6 +34,11 @@ const TArray<UTrafficLightComponent *> &UTrafficLightController::GetTrafficLight
return TrafficLights;
}
void UTrafficLightController::EmptyTrafficLights()
{
TrafficLights.Empty();
}
void UTrafficLightController::AddTrafficLight(UTrafficLightComponent * TrafficLight)
{
TrafficLights.Add(TrafficLight);

View File

@ -51,6 +51,9 @@ public:
UFUNCTION(Category = "Traffic Controller", BlueprintPure)
const TArray<UTrafficLightComponent *> &GetTrafficLights();
UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
void EmptyTrafficLights();
UFUNCTION(Category = "Traffic Controller", BlueprintPure)
const FString &GetControllerId() const;

View File

@ -23,19 +23,21 @@ ATrafficLightManager::ATrafficLightManager()
}
}
void ATrafficLightManager::RegisterLightComponent(UTrafficLightComponent * TrafficLight)
void ATrafficLightManager::RegisterLightComponent(UTrafficLightComponent * TrafficLightComponent)
{
// Cast to std::string
carla::road::SignId SignId(TCHAR_TO_UTF8(*(TrafficLight->GetSignId())));
carla::road::SignId SignId(TCHAR_TO_UTF8(*(TrafficLightComponent->GetSignId())));
// Get OpenDRIVE signal
if (GetMap()->GetSignals().count(SignId) == 0)
{
UE_LOG(LogCarla, Error, TEXT("Missing signal with id: %s"), *(SignId.c_str()) );
return;
}
const auto &Signal = GetMap()->GetSignals().at(SignId);
if(Signal->GetControllers().empty())
{
UE_LOG(LogCarla, Error, TEXT("No controllers in signal %s"), *(SignId.c_str()) );
return;
}
// Only one controller per signal
@ -45,6 +47,7 @@ void ATrafficLightManager::RegisterLightComponent(UTrafficLightComponent * Traff
const auto &Controller = GetMap()->GetControllers().at(ControllerId);
if(Controller->GetJunctions().empty())
{
UE_LOG(LogCarla, Error, TEXT("No junctions in controller %d"), *(ControllerId.c_str()) );
return;
}
// Get junction of the controller
@ -70,11 +73,13 @@ void ATrafficLightManager::RegisterLightComponent(UTrafficLightComponent * Traff
}
auto *TrafficLightController = TrafficControllers[ControllerId.c_str()];
TrafficLight->TrafficLightGroup = TrafficLightGroup;
TrafficLight->TrafficLightController = TrafficLightController;
TrafficLightComponent->TrafficLightGroup = TrafficLightGroup;
TrafficLightComponent->TrafficLightController = TrafficLightController;
std::cout << "ATrafficLightManager::RegisterLightComponent TrafficLightController" << TrafficLightController << std::endl;
// Add signal to controller
TrafficLightController->AddTrafficLight(TrafficLight);
TrafficLightController->AddTrafficLight(TrafficLightComponent);
TrafficLightController->ResetState();
// Add signal to map
@ -90,7 +95,8 @@ const boost::optional<carla::road::Map>& ATrafficLightManager::GetMap()
FString MapName = GetWorld()->GetName();
std::string opendrive_xml = carla::rpc::FromFString(UOpenDrive::LoadXODR(MapName));
Map = carla::opendrive::OpenDriveParser::Load(opendrive_xml);
if (!Map.has_value()) {
if (!Map.has_value())
{
UE_LOG(LogCarla, Error, TEXT("Invalid Map"));
}
}
@ -137,6 +143,27 @@ void ATrafficLightManager::GenerateTrafficLights()
FAttachmentTransformRules::KeepRelativeTransform);
}
}
TrafficLightsGenerated = true;
}
void ATrafficLightManager::RemoveGeneratedTrafficLights()
{
for(auto& Sign : TrafficSigns)
{
Sign->Destroy();
}
TrafficSigns.Empty();
for(auto& TrafficGroup : TrafficGroups)
{
TrafficGroup.Value->Destroy();
}
TrafficGroups.Empty();
TrafficControllers.Empty();
TrafficLightsGenerated = false;
}
// Called when the game starts
@ -144,6 +171,37 @@ void ATrafficLightManager::BeginPlay()
{
Super::BeginPlay();
if (TrafficLightsGenerated)
{
for(auto& It : TrafficControllers)
{
UTrafficLightController* Controller = It.Value;
Controller->EmptyTrafficLights();
}
for(auto& It : TrafficGroups)
{
ATrafficLightGroup* Group = It.Value;
Group->GetControllers().Empty();
}
for (TActorIterator<ATrafficSignBase> It(GetWorld()); It; ++It)
{
ATrafficSignBase* trafficSignBase = (*It);
UTrafficLightComponent* TrafficLightComponent =
trafficSignBase->FindComponentByClass<UTrafficLightComponent>();
if(TrafficLightComponent)
{
RegisterLightComponent(TrafficLightComponent);
}
}
}
else
{
GenerateTrafficLights();
}
}
ATrafficLightGroup* ATrafficLightManager::GetTrafficGroup(carla::road::JuncId JunctionId)
@ -167,9 +225,9 @@ UTrafficLightController* ATrafficLightManager::GetController(FString ControllerI
UTrafficLightComponent* ATrafficLightManager::GetTrafficLight(FString SignId)
{
if (!TrafficLights.Contains(SignId))
if (!TrafficLightComponents.Contains(SignId))
{
return nullptr;
}
return TrafficLights[SignId];
return TrafficLightComponents[SignId];
}

View File

@ -41,6 +41,9 @@ public:
UFUNCTION(CallInEditor)
void GenerateTrafficLights();
UFUNCTION(CallInEditor)
void RemoveGeneratedTrafficLights();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
@ -61,7 +64,10 @@ private:
// Mapped references to individual TrafficLightComponents
UPROPERTY()
TMap<FString, UTrafficLightComponent *> TrafficLights;
TMap<FString, UTrafficLightComponent *> TrafficLightComponents;
// Mapped references to TrafficSigns
TArray<ATrafficSignBase*> TrafficSigns;
UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
TSubclassOf<ATrafficSignBase> TrafficLightModel;
@ -70,4 +76,8 @@ private:
USceneComponent *SceneComponent;
boost::optional<carla::road::Map> Map;
UPROPERTY()
bool TrafficLightsGenerated = false;
};