I just started to learn to use network programming in a third person template game. One executes the following code after pressing a keyboard key:
void UMyGameInstance::OpenLobby()
{
UWorld* world = GetWorld();
if (world)
{
if (world->ServerTravel(FString("/Game/Maps/Lobby?listen")))
{
Log("OpenLobby sucess");
}
else
{
Log("OpenLobby failed");
}
}
else
{
Log("GetWorld failed");
}
}
This will create a “Lobby” level and listen.
The other executes the following code after pressing a keyboard key:
void UMyGameInstance::CallClientTravel(const FString& address)
{
APlayerController* playerController = GetFirstLocalPlayerController();
if (playerController)
{
playerController->ClientTravel(address, ETravelType::TRAVEL_Absolute);
}
else
{
Log("CallClientTravel failed");
}
}
So it should be able to jump to that “Lobby” level.
Use the independent process game to execute successively. However, sometimes you can jump to the “Lobby” level, but more often you just reload the current level. I am puzzled why it is not always successful or failed.
In addition, does ClientTravel() have an error callback function? I found that even if I intentionally input the first parameter to an IP address that does not exist, it will only reload the current level without returning any error.