Updated TL and TS to be spawned on sublevels

This commit is contained in:
doterop 2020-11-02 11:38:16 +01:00
parent 8f1b401e49
commit a0a2c83f13
3 changed files with 49 additions and 5 deletions

View File

@ -229,10 +229,13 @@ ATrafficLightManager* ACarlaGameModeBase::GetTrafficLightManager()
{
if (!TrafficLightManager)
{
AActor* TrafficLightManagerActor = UGameplayStatics::GetActorOfClass(GetWorld(), ATrafficLightManager::StaticClass());
UWorld* World = GetWorld();
AActor* TrafficLightManagerActor = UGameplayStatics::GetActorOfClass(World, ATrafficLightManager::StaticClass());
if(TrafficLightManagerActor == nullptr)
{
TrafficLightManager = GetWorld()->SpawnActor<ATrafficLightManager>();
FActorSpawnParameters SpawnParams;
SpawnParams.OverrideLevel = GetLevelToSpanInto("TrafficLights");
TrafficLightManager = World->SpawnActor<ATrafficLightManager>(SpawnParams);
}
else
{
@ -485,4 +488,27 @@ void ACarlaGameModeBase::ConvertMapLayerMaskToMapNames(int32 MapLayer, TArray<FN
}
}
}
}
ULevel* ACarlaGameModeBase::GetLevelToSpanInto(FString LevelName)
{
ULevel* OutLevel = nullptr;
UWorld* World = GetWorld();
const TArray <ULevelStreaming*> Levels = World->GetStreamingLevels();
for(ULevelStreaming* Level : Levels)
{
FString FullSubMapName = Level->PackageNameToLoad.ToString();
if(FullSubMapName.Contains(LevelName))
{
OutLevel = Level->GetLoadedLevel();
if(!OutLevel)
{
UE_LOG(LogCarla, Warning, TEXT("%s has not been loaded"), *LevelName);
}
break;
}
}
return OutLevel;
}

View File

@ -70,6 +70,9 @@ public:
UFUNCTION(Category = "Carla Game Mode", BlueprintCallable, CallInEditor, Exec)
void UnLoadMapLayer(int32 MapLayers);
UFUNCTION(Category = "Carla Game Mode")
ULevel* GetLevelToSpanInto(FString LevelName);
protected:
void InitGame(const FString &MapName, const FString &Options, FString &ErrorMessage) override;

View File

@ -53,6 +53,9 @@ ATrafficLightManager::ATrafficLightManager()
void ATrafficLightManager::RegisterLightComponentFromOpenDRIVE(UTrafficLightComponent * TrafficLightComponent)
{
ACarlaGameModeBase *GM = UCarlaStatics::GetGameMode(GetWorld());
check(GM);
// Cast to std::string
carla::road::SignId SignId(TCHAR_TO_UTF8(*(TrafficLightComponent->GetSignId())));
@ -78,8 +81,10 @@ void ATrafficLightManager::RegisterLightComponentFromOpenDRIVE(UTrafficLightComp
// Search/create TrafficGroup (junction traffic light manager)
if(!TrafficGroups.Contains(JunctionId))
{
FActorSpawnParameters SpawnParams;
SpawnParams.OverrideLevel = GM->GetLevelToSpanInto("TrafficLights");
auto * NewTrafficLightGroup =
GetWorld()->SpawnActor<ATrafficLightGroup>();
GetWorld()->SpawnActor<ATrafficLightGroup>(SpawnParams);
NewTrafficLightGroup->JunctionId = JunctionId;
TrafficGroups.Add(JunctionId, NewTrafficLightGroup);
}
@ -97,8 +102,10 @@ void ATrafficLightManager::RegisterLightComponentFromOpenDRIVE(UTrafficLightComp
}
else
{
FActorSpawnParameters SpawnParams;
SpawnParams.OverrideLevel = GM->GetLevelToSpanInto("TrafficLights");
auto * NewTrafficLightGroup =
GetWorld()->SpawnActor<ATrafficLightGroup>();
GetWorld()->SpawnActor<ATrafficLightGroup>(SpawnParams);
NewTrafficLightGroup->JunctionId = TrafficLightGroupMissingId;
TrafficGroups.Add(NewTrafficLightGroup->JunctionId, NewTrafficLightGroup);
TrafficLightGroup = NewTrafficLightGroup;
@ -439,6 +446,9 @@ void ATrafficLightManager::SpawnTrafficLights()
}
}
}
ACarlaGameModeBase *GM = UCarlaStatics::GetGameMode(GetWorld());
check(GM);
for(auto &SignalId : SignalsToSpawn)
{
// TODO: should this be an assert?
@ -468,6 +478,7 @@ void ATrafficLightManager::SpawnTrafficLights()
SpawnParams.Owner = this;
SpawnParams.SpawnCollisionHandlingOverride =
ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
SpawnParams.OverrideLevel = GM->GetLevelToSpanInto("TrafficLights");
ATrafficLightBase * TrafficLight = GetWorld()->SpawnActor<ATrafficLightBase>(
TrafficLightModel,
SpawnLocation,
@ -509,6 +520,9 @@ void ATrafficLightManager::SpawnTrafficLights()
void ATrafficLightManager::SpawnSignals()
{
ACarlaGameModeBase *GM = UCarlaStatics::GetGameMode(GetWorld());
check(GM);
const auto &Signals = GetMap()->GetSignals();
for (auto& SignalPair : Signals)
{
@ -559,6 +573,7 @@ void ATrafficLightManager::SpawnSignals()
SpawnParams.Owner = this;
SpawnParams.SpawnCollisionHandlingOverride =
ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
SpawnParams.OverrideLevel = GM->GetLevelToSpanInto("TrafficSigns");
ATrafficSignBase * TrafficSign = GetWorld()->SpawnActor<ATrafficSignBase>(
TrafficSignsModels[SignalType],
SpawnLocation,