Skip to content

Commit efe2d23

Browse files
author
Jonathan
authored
Merge pull request #4599 from talha-opteran/patch-1
Fix load level for UE editor
2 parents dfedae9 + ebe26a6 commit efe2d23

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,19 @@ void UAirBlueprintLib::setSimulatePhysics(AActor* actor, bool simulate_physics)
7676

7777
bool UAirBlueprintLib::loadLevel(UObject* context, const FString& level_name)
7878
{
79-
FString LongPackageName;
80-
const bool success = FPackageName::SearchForPackageOnDisk(level_name, &LongPackageName);
81-
if (success) {
82-
context->GetWorld()->SetNewWorldOrigin(FIntVector(0, 0, 0));
83-
UGameplayStatics::OpenLevel(context->GetWorld(), FName(*LongPackageName), true);
79+
80+
bool success{ false };
81+
// Get name of current level
82+
auto currMap = context->GetWorld()->GetMapName();
83+
currMap.RemoveFromStart(context->GetWorld()->StreamingLevelsPrefix);
84+
// Only load new level if different from current level
85+
if (!currMap.Equals(level_name)) {
86+
FString LongPackageName;
87+
success = FPackageName::SearchForPackageOnDisk(level_name, &LongPackageName);
88+
if (success) {
89+
context->GetWorld()->SetNewWorldOrigin(FIntVector(0, 0, 0));
90+
UGameplayStatics::OpenLevel(context->GetWorld(), FName(*LongPackageName), true);
91+
}
8492
}
8593
return success;
8694
}

Unreal/Plugins/AirSim/Source/SimHUD/SimHUD.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ void ASimHUD::BeginPlay()
2222

2323
try {
2424
UAirBlueprintLib::OnBeginPlay();
25+
initializeSettings();
2526
loadLevel();
2627

27-
initializeSettings();
28+
// Prevent a MavLink connection being established if changing levels
29+
if (map_changed_) return;
30+
2831
setUnrealEngineSettings();
2932
createSimMode();
3033
createMainWidget();
@@ -253,7 +256,7 @@ std::string ASimHUD::getSimModeFromUser()
253256

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

259262
void ASimHUD::createSimMode()

Unreal/Plugins/AirSim/Source/SimHUD/SimHUD.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@ class AIRSIM_API ASimHUD : public AHUD
7373
ASimModeBase* simmode_;
7474

7575
APIPCamera* subwindow_cameras_[AirSimSettings::kSubwindowCount];
76+
bool map_changed_;
7677
};

0 commit comments

Comments
 (0)