Skip to content

Commit

Permalink
Merge pull request microsoft#4599 from talha-opteran/patch-1
Browse files Browse the repository at this point in the history
Fix load level for UE editor
  • Loading branch information
Jonathan authored Jul 17, 2022
2 parents dfedae9 + ebe26a6 commit efe2d23
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
18 changes: 13 additions & 5 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,19 @@ void UAirBlueprintLib::setSimulatePhysics(AActor* actor, bool simulate_physics)

bool UAirBlueprintLib::loadLevel(UObject* context, const FString& level_name)
{
FString LongPackageName;
const bool success = FPackageName::SearchForPackageOnDisk(level_name, &LongPackageName);
if (success) {
context->GetWorld()->SetNewWorldOrigin(FIntVector(0, 0, 0));
UGameplayStatics::OpenLevel(context->GetWorld(), FName(*LongPackageName), true);

bool success{ false };
// Get name of current level
auto currMap = context->GetWorld()->GetMapName();
currMap.RemoveFromStart(context->GetWorld()->StreamingLevelsPrefix);
// Only load new level if different from current level
if (!currMap.Equals(level_name)) {
FString LongPackageName;
success = FPackageName::SearchForPackageOnDisk(level_name, &LongPackageName);
if (success) {
context->GetWorld()->SetNewWorldOrigin(FIntVector(0, 0, 0));
UGameplayStatics::OpenLevel(context->GetWorld(), FName(*LongPackageName), true);
}
}
return success;
}
Expand Down
7 changes: 5 additions & 2 deletions Unreal/Plugins/AirSim/Source/SimHUD/SimHUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ void ASimHUD::BeginPlay()

try {
UAirBlueprintLib::OnBeginPlay();
initializeSettings();
loadLevel();

initializeSettings();
// Prevent a MavLink connection being established if changing levels
if (map_changed_) return;

setUnrealEngineSettings();
createSimMode();
createMainWidget();
Expand Down Expand Up @@ -253,7 +256,7 @@ std::string ASimHUD::getSimModeFromUser()

void ASimHUD::loadLevel()
{
UAirBlueprintLib::RunCommandOnGameThread([&]() { UAirBlueprintLib::loadLevel(this->GetWorld(), FString(AirSimSettings::singleton().level_name.c_str())); }, true);
UAirBlueprintLib::RunCommandOnGameThread([&]() { this->map_changed_ = UAirBlueprintLib::loadLevel(this->GetWorld(), FString(AirSimSettings::singleton().level_name.c_str())); }, true);
}

void ASimHUD::createSimMode()
Expand Down
1 change: 1 addition & 0 deletions Unreal/Plugins/AirSim/Source/SimHUD/SimHUD.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ class AIRSIM_API ASimHUD : public AHUD
ASimModeBase* simmode_;

APIPCamera* subwindow_cameras_[AirSimSettings::kSubwindowCount];
bool map_changed_;
};

0 comments on commit efe2d23

Please sign in to comment.