Get Game Instance in UUserWidget class

How can I get a game instance from this class?

	if (GEngine->GetWorld() != nullptr && GEngine->GetWorld()->GetGameInstance() != nullptr)
	{
		UGameInstanceCPP* SGI = Cast<UGameInstanceCPP>(GEngine->GetWorld()->GetGameInstance());
		SGI->Test();
	}

I was recently having this problem but found that removing “GEngine” from the cast fixed my problem.

If you’re still having trouble try using something like this instead:

UGameInstanceCPP* SGI = Cast<UGameInstanceCPP>(GetWorld()->GetGameInstance());

if(IsValid(SGI))
{
     // Do Stuff
}