Custom GameInstance EndPlay

I want to override BeginDestroy() function of UObject, wich is parent class for UGameInstance, namely grandparent for my c++ class.
That for add to my custom gameInstance blueprint native event like the EndPlay event in AActor.

Here is my code, so how to make it work?

MyGameInstance.h

#pragma once

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

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

public:

   virtual void BeginDestroy() override;

   UFUNCTION(BlueprintNativeEvent)
   void MyGameLogout();
   virtual void MyGameLogout_Implementation();
};

MyGameInstance.cpp

#include "MyProject.h"
#include "MyGameInstance.h"



void UMyGameInstance::BeginDestroy()
{
   MyGameLogout(); // <-exactly this string crashes ue on save, compile and reparent blueprint (without it all works)

   Super::BeginDestroy();
}

void UMyGameInstance::MyGameLogout_Implementation()
{
}

that to take gameInstance’s variables on game quit before they will erased

Hey lllplus-

Without knowing what the MyGameLogout() function is doing in your blueprint, my suggestion would be to call the function after Super::BeginDestroy(). If there are still issues then please provide a screenshot of MyGameLogout in your blueprint as well as the crash info including callstack and log files.

Hello ) thx for support

For now MyGameLogout() event in blueprint does nothing (more pricesely- just prints ‘hello’). For understanding why i need it- i want to collect some variables, which stored in gameInstance and transfer they to playerState, then send http request(with this variables) from playerState(via vaRest plugin) to my web site, that for mark players as offline(in site’s database) on game quit. I tryed to use EndPlay event in playerState, but when it is called- gameInstance with all variables are already erased( So i need in gameInstance event like EndPlay in any actor derived class.

I was looking for logs files before, but never dealt with it and in some paths that i have seen was nothing.

Now i found it in \MyProject\Saved\Logs:

[2016.11.02-15.48.31:531][654]LogWindows:Error: === Critical error: ===
[2016.11.02-15.48.31:531][654]LogWindows:Error:
[2016.11.02-15.48.31:531][654]LogWindows:Error: Assertion failed: !IsUnreachable() [File:D:\Build++UE4+Release-4.13+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp] [Line: 1124]
[2016.11.02-15.48.31:531][654]LogWindows:Error: MyGameInstance /Engine/Transient.None Function: ‘/Script/MyProject.MyGameInstance:MyGameLogoutSendRequest’

‘call the function after Super::BeginDestroy()’ - doesn’t help :frowning:

Hey lllplus-

The issue appears to derive from calling the BlueprintNativeEvent inside the BeginDestroy() function. The crash received states “Is Unreachable” which indicates that it can’t find the event since the game instance has started to destroy itself. Typically custom code inside a “Destroy” or “Delete” function should be limited to freeing memory if necessary to prevent attempting to access something that no longer exists.

For the use that you mentioned, you can use your BlueprintNativeEvent in the Shutdown() function which will avoid the crash altogether.

Cheers

Yes, its exactly what i need!
Thank you very much :slight_smile: