Download

Blueprint Accessed None on C++ class member after returning to menu

I’m creating a class to hold game stat information (stuff like high score, fastest times, etc). I have a menu game type that holds a gameStat variable so that it can be accessed by Blueprints for the UI Widgets. When I play in editor the first time the menu loads, it can access the gameStat variable fine and all the information shows up in the Widget. If I play the game (Open Level) and then come back to the menu (also Open Level) I start getting BP errors LogScript:Warning: Accessed None ‘gameStats’. It seems the gameStats variable is not getting re-initialized when the game loads the menu again. I’m not sure why. This only happens in PIE. If I play as standalone it works as expected.

Here’s how the variable is defined.

UPROPERTY(BlueprintReadOnly)
TWeakObjectPtr<UGameStats> gameStats; // Contains information about past games including best times data.

Here’s how the menu class is constructed

AMenuGameMode::AMenuGameMode(const FObjectInitializer& ObjectInitializer)
:Super( ObjectInitializer)
{

// use our own player controller class
PlayerControllerClass = AMyGamePlayerController::StaticClass();

// set some defaults
difficulty = EGameDifficulty::Easy;

// setup the game stats object.
gameStats = ObjectInitializer.CreateDefaultSubobject<UGameStats>(this, TEXT(“GameStats”));

}

Any help appreciated.

You are using TWeakObjectPtr. It’s unclear from your post whether UPROPERTY definition belongs to AMenuGameMode class or not.
If you don’t have any strong reference (directly by pointer) to your UGameStats anywhere else it may be garbage collected.

Ah. Thx. I misunderstood the purpose of TWeakObjectPtr. Converted to raw pointer and all is well.