Change to listen server without map change

Hello UE4 community!

I want to know if there is a way to start a listen server without opening a map. I want to start listening in the current map without reloading it.

Any idea??.

I have the same problem did you find any solution?..

Hey! I know this is an old post however I came here also looking for this answer and never found one!

I’ve managed to get this working correctly, and so far without any bugs too!

What im doing is creating my session and on success, I’m calling into C++ to open up the current level to listen without reloading like so:

void URPGGameInstanceAdvanced::OnHostSuccess(UObject* WorldContextObject, FString levelName)
{
	UWorld* currentWorld = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
	FURL newURL = FURL(*levelName);
	currentWorld->Listen(newURL);
}

Now i’m for sure theres ways of improving this but i’ll get back to that!

And the way I found to close the server without reloading is like so, firstly use the Destroy session call and then call into this:

void URPGGameInstanceAdvanced::CloseRPGSession(UObject* WorldContextObject, FString levelName)
{
	// TODO: return all other players with correct info as to why


	UWorld* currentWorld = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
	if (currentWorld->IsServer())
	{
		UE_LOG(LogTemp, Warning, TEXT("Shutting down net driver"));
		UNetDriver* driver = currentWorld->GetNetDriver();

		GEngine->DestroyNamedNetDriver(currentWorld, driver->NetDriverName);
		currentWorld->SetNetDriver(nullptr);
		FLevelCollection* SourceCollection = currentWorld->FindCollectionByType(ELevelCollectionType::DynamicSourceLevels);
		if (SourceCollection)
		{
			SourceCollection->SetNetDriver(nullptr);
		}
		FLevelCollection* StaticCollection = currentWorld->FindCollectionByType(ELevelCollectionType::StaticLevels);
		if (StaticCollection)
		{
			StaticCollection->SetNetDriver(nullptr);
		}
	}
}

Maybe theres a function somewhere that does this all for me but this is how, with modification, UWorld shuts down the NetDriver. The reason you have to do this is because if you try to rehost after closing, you’ll get a network error thrown and will be reloaded.

Lastly! I found this out recently but if you decorate your function like so, you’ll get the required WorldContextObject:

	UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject"), Category = "RPGInstance")
	void OnHostSuccess(UObject* WorldContextObject, FString levelName);
2 Likes