How to properly end the server?

I spent a few hours trying to find a way to exit the server properly.

But the server does not close. It is the same behaviour that is shown when using ServerTravel(“mainmenu”).

What happens is in my code below, the clients get kicked back to main menu before the host does. Which is all fine and dandy where the clients are definitely not in the server anymore. However, what is not good is that if the client joins again, while the original host is in main menu, the clients get pulled to mainmenu level. I want it to not be able to join a server at all. I want the server to be properly closed.

Side note: if you have a way of checked the text field for null and whether a server address is valid (failed to load) can you show me? right now if i try join the server while it is not open, then i try to instead host then it crashes the game.

my current code:

void UPuzzlePlatformsGameInstance::LeaveServer()
{
	if (!ensure(GEngine != nullptr)) return;

	GEngine->AddOnScreenDebugMessage(0, 10.f, FColor::Green, FString::Printf(TEXT("Leaving...")));
	UE_LOG(LogTemp, Warning, TEXT("Left Server Run"));

	APlayerController* PlayerController = GetFirstLocalPlayerController();
	if (!ensure(PlayerController != nullptr)) return;

	const UWorld* World = GetWorld();
	if (!ensure(World != nullptr)) return;

	if (World->IsServer()) 
	{
		GEngine->AddOnScreenDebugMessage(0, 10.f, FColor::Green, FString::Printf(TEXT("Host Left Server...")));
		
		TArray<AActor*> FoundControllers;
		UGameplayStatics::GetAllActorsOfClass(GetWorld(), APlayerController::StaticClass(), FoundControllers);

		for (AActor* Actor : FoundControllers)
		{
			APlayerController* Controller = Cast<APlayerController>(Actor);
			if (Controller != nullptr && Controller != PlayerController) {
				Controller->ClientTravel("/Game/MenuSystem/MainMenu?listen", ETravelType::TRAVEL_Absolute);
			}
		}
	}

	PlayerController->ClientTravel("/Game/MenuSystem/MainMenu?listen", ETravelType::TRAVEL_Absolute);
}
2 Likes

If I recall correctly, this is covered later, in section 3. It is a good question however. This really makes more sense when you start dealing with the likes of steam or EOS which is why I refer to section 3.

I would continue working through the course and hopefully this all makes sense when you do start working with Steam.

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms