Game Instance and AActor variable

Hey guys,

So I’ve been working on a basic game for the Windows Platform lately, and I have a level transition system prototype roughed out, where it would Serialize player data to a binary file and load it back into the player character on level load.

Now I came across Ramas wiki tutorial: Game Instance, Custom Game Instance For Inter-Level Persistent Data Storage, not knowing of Game Instance. Now I’ve done a few successful tests with my own Game Instance class, with basic types such as FStrings and uint32. Obviously these values remain persistent on level changes, but I am wanting to work with UObjects, specifically a derived ACharacter class. Consider the following:

//Game Instance .h

public:

        UPROPERTY(EditAnywhere, BlueprintReadWrite)
        AplayerCharacter * playerStorage;

//On Level Load in playerCharacter.cpp

        UMainGameInstance * inst = Cast<UMainGameInstance>(GetGameInstance());

        if(!inst)
        {
            //LogFunction
            return;
        }

        inst->playerStorage = this;

Now assigning and testing playerStorage variables work in the scope that it was declared in, but if I try the following:

        UMainGameInstance * inst = Cast<UMainGameInstance>(GetGameInstance());
    
         if(!inst)
         {
            //LogFunction
            return;
         }
    
         FString testString = inst->playerStorage->GetCustomName();

Will always toggle a breakpoint in Visual Studio(As I am debugging in DebugGame Editor). For some reason, and I don’t know why, inst->playerStorage is always NULL outside the scope when I assigned its value. Yet if I used say an actual FString, it will work.

This got me onto thinking, if there is no way to achieve this, would I be better off creating a structure with all the variables from my AplayerCharacter class and just use the structure in the game instance class?

Note* In small words, I am trying to store my player data for loading a level, then loading the player data back in, without saving to file.

Edit * Using structures accomplishes this, but with manually writing out all the variables. So I would like to know if I can store a copy of the AplayerCharacter in the game instance class, and have it work out of scope?

Thanks Guys,

So far this suffices as a solution to my problem.

 struct FcharData
    {
    	GENERATED_USTRUCT_BODY()
    
    public:
    
    	UPROPERTY()
    	FString playerName;
    
    	UPROPERTY()
    	bool bPlayerBeganGame;
    
    	UPROPERTY()
    	FString LastLevelName;
    
    	UPROPERTY()
    	FString currentLevel;
    
    	UPROPERTY()
    	bool bIsTransitioning;
    
    	UPROPERTY()
    	bool bLoadedFromSave;
    
    	FcharData()
    	{
    		playerName = "";
    		bPlayerBeganGame = false;
    		LastLevelName = "";
    		currentLevel = "";
    		bIsTransitioning = false; 
    		bLoadedFromSave = false;
    	}
    
    	FcharData(AplayerCharacter * inPlayer)
    	{
    		playerName = inPlayer->PlayerName;
    		bPlayerBeganGame = inPlayer->bPlayerBeganGame;
    		LastLevelName = inPlayer->LastLevelName;
    		currentLevel = inPlayer->currentLevel;
    		bIsTransitioning = inPlayer->bIsPlayerTransitioningLevel;
    		bLoadedFromSave = inPlayer->bLoadedFromSave;
    	}
    
    	void FillPlayerData(AplayerCharacter & inPlayer)
    	{
    		inPlayer.PlayerName = playerName;
    		inPlayer.bPlayerBeganGame = bPlayerBeganGame;
    		inPlayer.LastLevelName = LastLevelName;
    		inPlayer.currentLevel = currentLevel;
    		inPlayer.bIsPlayerTransitioningLevel = bIsTransitioning;
    		inPlayer.bLoadedFromSave = bLoadedFromSave;
    	}
    };

Though I am wondering why my original idea of AplayerCharacter * player = this; is not working. Could ‘player’ be getting garbage collected? seeings as it is a pointer.

Hey Aaron, i need some help. In the same Rama tutorial how did you used
USolusGameInstance* SGI = Cast(GetGameInstance()); if(SGI) {
SGI->InterLevelPersistentValue++;
ClientMessage(FString::FromInt(SGI->InterLevelPersistentValue));
}`”
is there any intialization of SGI or USolusGameInstance or cast, if yes then where and which data types?

because iam trying to use InterLevelPersistentValue in another class, but dont know how to do it.