Network and Travel error handling

I never trust anything networky to actually work or for the user to type anything in correctly :slight_smile: so I did some research into how Unreal handles that sort of thing. I think this is the way to do it, in other languages / lives I’d usually just do some try catch blocks etc. or check the call if it returned true etc. but none of those options were available.

Unreal seems to do it on an event based system, so using what we have done so far binding events and a lot of searching! I went for this in the game instance class. I’d be interested to know if this is the correct approach, it seems to work (If i was doing it for real I’d do some client side checking as well before this this is just catch all)

In the header file forward declare one function for network and one for travel as I noticed when you call ClientTravel it will try and load a map name if it cant parse the IP so you get different errors.

From the docs I found the format needed to be

	UFUNCTION()
	void HandleNetworkFailure(UWorld* World, UNetDriver* NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString);
	UFUNCTION()
	void HandleTravelFaliure(UWorld* World, ETravelFailure::Type FailureType, const FString& ErrorString);

Then in the cpp file create a couple of functions to receive the events, I just did a basic menu reset but whatever.

void UPuzzlePlatformsGameInstance::HandleNetworkFailure(UWorld* World, UNetDriver* NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString)
{
	UE_LOG(LogTemp, Warning, TEXT("NetworkError %s"), *ErrorString);
	if (Menu != nullptr)
	{
		Menu->Teardown();
		Menu->Setup();
	}

}

void UPuzzlePlatformsGameInstance::HandleTravelFaliure(UWorld* World, ETravelFailure::Type FailureType, const FString& ErrorString)
{
	UE_LOG(LogTemp, Warning, TEXT("TravelError %s"), *ErrorString);
	if (Menu != nullptr)
	{
		Menu->Teardown();
		Menu->Setup();
	}

}

Then in Init I bind the events to the functions

void UPuzzlePlatformsGameInstance::Init()
{
	GEngine->OnNetworkFailure().AddUObject(this, &UPuzzlePlatformsGameInstance::HandleNetworkFailure);
	GEngine->OnTravelFailure().AddUObject(this, &UPuzzlePlatformsGameInstance::HandleTravelFaliure);

}

If you test it in the editor context the editor kills the session regardless with a warning but if you run it from command line / compile it works as expected, the event fires and runs the code.

5 Likes

Privacy & Terms