CatsNipYummy, the answer I gave above was from old code that I don’t have any more to look at. You can try rearranging the bit of code I have up there to do a null check after getting game instance instead of before.
gameInstance = Cast(GEngine->GetWorld()->GetGameInstance());
if (gameInstance)
{
do stuff;
}
else
{
log error
}
Other working code that I have in an old 4.9 project had a couple ways that I obtained the game instance.
AMyGameGameMode* gameMode = (AMyGameGameMode*)GetWorld()->GetAuthGameMode();
if (gameMode)
{
UMyGameGameInstance* gameInstance = Cast<UMyGameGameInstance>(gameMode->GetGameInstance());
if (gameInstance)
{
do stuff
}
Also, within my components, I had
UMyGameGameInstance *gameInstance = Cast<UMyGameGameInstance>(GetParentActor()->GetGameInstance());
if (gameInstance)
{
do stuff;
}
else
{
log error
}
One thing to keep in mind with this last one, GetParentActor( ) was a function I created, not one that’s in the engine. It was simply storing a handle to the parent actor. That handle was passed to it, but with engine calls, I think you could do something like (AActorClass*)GetOwner() if you’re in a component. Otherwise, the above code should work with any actor you can get access to.