Access GameInstance singleton from UMG UUserWidget

Hi all,

We are looking for a way to call functions in our singleton GameInstance from custom UUserWidgets. We currently have a lua state which is created in the GameInstance that we need to access statically from our game interface to pass function calls to Lua. As UUserWidgets are not actors we cannot use GetGameInstance() to reference the GameInstance.

Is there an alternative to passing in/spawning an Actor and taking the game instance form that? Or is there a better alternative to create a singleton in which to create our Lua state, which can have functions called in it by UUserWidgets statically.

Cheers.

Hi,

you could try accessing it from

static UGameInstance *UGameplayStatics::GetGameInstance(UObject* WorldContextObject)

This static function needs a world context object in order to recover the world’s game instance.
you could try something like:

UGameInstance* yourInstance = UGameplayStatics::GetGameInstance(GetWorld());

as UUserWidget has a GetWorld() method. You could even try this:

UGameInstance* yourInstance = GetWorld()->GetGameInstance();

Hope this helps.