From e608c8c55f3cf2bb3fb0c000b67329e3716e65e0 Mon Sep 17 00:00:00 2001 From: bernatx Date: Mon, 5 Dec 2022 18:16:27 +0100 Subject: [PATCH] Add command to LOAD_MAP in multi-gpu mode --- .../source/carla/multigpu/primaryCommands.cpp | 6 +++--- .../Carla/Source/Carla/Game/CarlaEngine.cpp | 5 ++++- .../Plugins/Carla/Source/Carla/Game/CarlaEngine.h | 5 +++++ .../Carla/Source/Carla/Game/CarlaEpisode.cpp | 15 +++++++++++++++ .../Carla/Source/Carla/Game/CarlaGameInstance.h | 5 +++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/LibCarla/source/carla/multigpu/primaryCommands.cpp b/LibCarla/source/carla/multigpu/primaryCommands.cpp index 285183d62..a6c3b9be2 100644 --- a/LibCarla/source/carla/multigpu/primaryCommands.cpp +++ b/LibCarla/source/carla/multigpu/primaryCommands.cpp @@ -31,9 +31,9 @@ void PrimaryCommands::SendFrameData(carla::Buffer buffer) { } // broadcast to all secondary servers the map to load -void PrimaryCommands::SendLoadMap(std::string) { - // carla::Buffer buf((unsigned char *) map.c_str(), (size_t) map.size()); - log_info("sending load map command"); +void PrimaryCommands::SendLoadMap(std::string map) { + carla::Buffer buf((unsigned char *) map.c_str(), (size_t) map.size() + 1); + _router->Write(MultiGPUCommand::LOAD_MAP, std::move(buf)); } // send to who the router wants the request for a token diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.cpp index 36a5ccc41..a1e58821f 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.cpp @@ -127,8 +127,11 @@ void FCarlaEngine::NotifyInitGame(const UCarlaSettings &Settings) break; } case carla::multigpu::MultiGPUCommand::LOAD_MAP: + { + FString FinalPath((char *) Data.data()); + UGameplayStatics::OpenLevel(CurrentEpisode->GetWorld(), *FinalPath, true); break; - + } case carla::multigpu::MultiGPUCommand::GET_TOKEN: { // get the sensor id diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.h index 2644db442..04fa54213 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.h @@ -77,6 +77,11 @@ public: FCarlaEngine::FrameCounter = Value; } + std::shared_ptr GetSecondaryServer() + { + return SecondaryServer; + } + private: void OnPreTick(UWorld *World, ELevelTick TickType, float DeltaSeconds); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp index d44307788..010abd701 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp @@ -103,6 +103,21 @@ bool UCarlaEpisode::LoadNewEpisode(const FString &MapString, bool ResetSettings) UGameplayStatics::OpenLevel(GetWorld(), *FinalPath, true); if (ResetSettings) ApplySettings(FEpisodeSettings{}); + + // send 'LOAD_MAP' command to all secondary servers (if any) + if (bIsPrimaryServer) + { + UCarlaGameInstance *GameInstance = UCarlaStatics::GetGameInstance(GetWorld()); + if(GameInstance) + { + FCarlaEngine *CarlaEngine = GameInstance->GetCarlaEngine(); + auto SecondaryServer = CarlaEngine->GetSecondaryServer(); + if (SecondaryServer->HasClientsConnected()) + { + SecondaryServer->GetCommander().SendLoadMap(std::string(TCHAR_TO_UTF8(*FinalPath))); + } + } + } } return bIsFileFound; } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameInstance.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameInstance.h index f5159dd01..68c9977bb 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameInstance.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameInstance.h @@ -108,6 +108,11 @@ public: return CurrentMapLayer; } + FCarlaEngine* GetCarlaEngine() + { + return &CarlaEngine; + } + private: UPROPERTY(Category = "CARLA Settings", EditAnywhere)