Using HandleNetworkFailure In C++

Hi :wave:

I’m trying to get the HandleNetworkFailure event to work in my Game Instance in C++. I’m doing this because the Event Network Error node in blueprints is missing an output for the Error String for some odd reason.

ENENode

I’ve got this in my Game Instance’s .h file:

#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "Engine/Engine.h"
#include "MyGameInstance.generated.h"

UCLASS()
class TESTPROJECT_API UMyGameInstance : public UGameInstance
{
	GENERATED_BODY()

public:

	virtual void Init() override;

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

};

And this in my Game Instance’s .cpp file:

#include "Engine/Engine.h"
#include "MyGameInstance.h"

void UMyGameInstance::Init()
{
	Super::Init();
	// Bind Network errors
	GetEngine()->OnNetworkFailure().AddUObject(this, &UMyGameInstance::HandleNetworkFailure);
}

void UMyGameInstance::HandleNetworkFailure(UWorld * World, UNetDriver * NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString)
{
	UE_LOG(LogTemp, Warning, TEXT("Custom network error event works!"));
}

The idea is that it would print Custom network error event works! to the server log when the event is fired. That way I’ll know it works and I can take it from there.

Everything compiles fine, but nothing seems to happen - nothing is printed to the log.

Does anyone know what I’m doing wrong? :woozy_face:

Where are you getting this HandleNetworkFailure from ? If you’re trying to override a base implementation then you’re missing the override keyword.

I’m trying to override a base implementation but when I add override; at the end I get this error during compilation:

Severity	Code	Description	Project	File	Line	Suppression State
Error	C3668	'UMyGameInstance::HandleNetworkFailure': method with override specifier 'override' did not override any base class methods

I’m new to C++ and I have no idea what that means, but I think that this event can’t be overridden. :man_shrugging:

Override is not necessary in C++ so it can’t be the issue.

The GameInstance::HandleNetworkError is a BlueprintImplementableEvent which means it is only available in Blueprint.

You should however override UEngine::HandleNetworkFailure instead and remember to set the DefaultEngine.ini to use your custom UEngine class.

2 Likes

Oh, I understand.

Would you be kind enough to show me how to override UEngine::HandleNetworkFailure, please? :flushed: I’m new to C++ and I don’t know what to write in the .h file to get it to work and I’m sure that a lot of others that’ll be looking for this in the future won’t either.

What is my base class? Is it my Game Mode class?

In your .h file

UFUNCTION()
void NetworkFailureHappened(UWorld* InWorld, UNetDriver* NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString);

In your CPP file:

void UFragileGameInstance::NetworkFailureHappened(UWorld* InWorld, UNetDriver* NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString) {
// do some logic
}

But, before this can work, you need to register yourself to network failure. You do it like this:

GEngine->OnNetworkFailure().AddUObject(this, &UMyClass::NetworkFailureHappened);

Of curse, you replace &UMyClass with class where you created method.

Thanks a lot! :blush: I tried to do exactly what you wrote even before I started this thread but it didn’t work because I don’t know where to put GEngine->OnNetworkFailure().AddUObject(this, &UMyClass::NetworkFailureHappened);

I tried to put it in the .cpp file where you wrote // do some logic but that didn’t work. Where should I put that line?

It has to be executed in order to work. You can move all this logic to your game instance, and register this in Init() method of your game instance. Works for me, just did some logic about that 5 mins ago :).
Since this is error handling that can happen at any time of the app (in game, looby… etc), maybe it’s best to put it in the GameInstance, so it’s alive while the game is alive.
So your game instance .h file:

virtual void Init() override;

Your game instance .cpp file

void UYourGameInstance::Init() {
    GEngine->OnNetworkFailure().AddUObject(this, &UYourGameInstance::NetworkFailureHappened);
}

Move also NetworkFailureHappened to your game instance, and it has to work.

2 Likes

Alright everyone in the future, I’ve gotten it to work with the help of the kind people above. This is how your .h file should look (Change: MyGameInstance & MYPROJECT to what corresponds to your own project).

#pragma once

#include "Engine/GameInstance.h"
#include "CoreMinimal.h"
#include "Engine/Engine.h"
#include "MyGameInstance.generated.h"

UCLASS()
class MYPROJECT_API UMyGameInstance : public UGameInstance
{
	GENERATED_BODY()

public:

	virtual void Init() override;

	// Our custom OnNetworkFailure event which also returns the error string.
	UFUNCTION(BlueprintNativeEvent)
	void NetworkFailureHappened(UWorld* InWorld, UNetDriver* NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString);

};

This is what your .cpp file should look like:

#include "MyGameInstance.h"
#include "Engine/Engine.h"

// Bind custom NetworkFailureHappened event to default OnNetworkFailure event 
void UMyGameInstance::Init()
{
	GEngine->OnNetworkFailure().AddUObject(this, &UMyGameInstance::NetworkFailureHappened);
}


// Our custom OnNetworkFailure event which also returns error string. 
void UMyGameInstance::NetworkFailureHappened_Implementation(UWorld* InWorld, UNetDriver* NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString)
{

}

This will also give you a new event in your Game Instance blueprint called Network Failure Happened

Node

Thanks for the help everyone, much appreciated!

4 Likes

Hello I tried your solution but I am getting this error when I am trying to compile I didn’t have it before

1>[1/4] Compile EOSgameInstance.cpp
1>C:\DFC\Source\DFCFromScratch\Private\EOSgameInstance.cpp(84): warning C4996: 'FTicker': Please use thread-safe FTSTicker, see the migration guide in the code comment above Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.
1>C:\DFC\Source\DFCFromScratch\Private\EOSgameInstance.cpp(84): warning C4996: 'FTicker::AddTicker': Please use thread-safe FTSTicker, see the migration guide in the code comment above Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.
1>C:\DFC\Source\DFCFromScratch\Private\EOSgameInstance.cpp(161): warning C4996: 'FTicker': Please use thread-safe FTSTicker, see the migration guide in the code comment above Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.
1>C:\DFC\Source\DFCFromScratch\Private\EOSgameInstance.cpp(161): warning C4996: 'FTicker::RemoveTicker': Please use thread-safe FTSTicker, see the migration guide in the code comment above Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.
1>[2/4] Link UnrealEditor-DFCFromScratch.lib

Thanks! I’m trying this on 5.3 but I’m getting linker errors about missing symbols…
To be precise:

0>   Creating library PROJECTFOLDER\Intermediate\Build\Win64\x64\UnrealEditor\Development\Project\UnrealEditor-Project.sup.lib and object PROJECTFOLDER\Intermediate\Build\Win64\x64\UnrealEditor\Development\Project\UnrealEditor-Project.sup.exp
0>MyGameInstance.gen.cpp.obj: Error  : LNK2019: unresolved external symbol "__declspec(dllimport) class UEnum * __cdecl Z_Construct_UEnum_NetCore_ENetworkFailure(void)" (__imp_?Z_Construct_UEnum_NetCore_ENetworkFailure@@YAPEAVUEnum@@XZ) referenced in function "void __cdecl `dynamic initializer for 'public: static struct UECodeGen_Private::FBytePropertyParams const Z_Construct_UFunction_USVGameInstance_NetworkFailureHappened_Statics::NewProp_FailureType''(void)" (??__E?NewProp_FailureType@Z_Construct_UFunction_USVGameInstance_NetworkFailureHappened_Statics@@2UFBytePropertyParams@UECodeGen_Private@@B@@YAXXZ)
0>MyGameInstance.cpp.obj: Error  : LNK2019: unresolved external symbol "__declspec(dllimport) class UEnum * __cdecl StaticEnum<enum ENetworkFailure::Type>(void)" (__imp_??$StaticEnum@W4Type@ENetworkFailure@@@@YAPEAVUEnum@@XZ) referenced in function "public: virtual void __cdecl USVGameInstance::NetworkFailureHappened_Implementation(class UWorld *,class UNetDriver *,enum ENetworkFailure::Type,class FString const &)" (?NetworkFailureHappened_Implementation@USVGameInstance@@UEAAXPEAVUWorld@@PEAVUNetDriver@@W4Type@ENetworkFailure@@AEBVFString@@@Z)
0>UnrealEditor-Project.dll: Error  : LNK1120: 2 unresolved externals

Found it - needed to add “NetCore” in PublicDependencyModuleNames in build.cs :slight_smile: