Access Game Instance from Object class?

I get that I can’t access the Game Instance from a Primary Data Asset (though it would be nice), but why can’t I access it from a standard Object class?

Quite a few tutorials say that it’s best practice to store commonly used variables and general game related things in the Game Instance, so why can I only make use of that in Actors?

I want to make a data class which has some sense of context, yet I’m locked out of much of my game’s data… having to initialize the class when used as a variable with the Game Instance feels yucky. Anyone have a cleaner workaround?

Common way to get GameInstance anywhere in C++ is UGameplayStatics::GetGameInstance(). There’s probably a blueprint for it as well.

UGameplayStatics::GetGameInstance | Unreal Engine Documentation

Thanks, but I asked in Blueprints. :slight_smile:

And sigh, of course there’s a C++ way. Just more proof that Epic doesn’t care about people who want to use Blueprints. :weary:

Is this it?

Get Game Instance | Unreal Engine Documentation

Some nodes (such as GetGameInstance) need a valid WorldContext in order to do their task. And so if you try to call this from an object that does not provide this WorldContext (needs to override GetWorld in C++) blueprints prevent you from calling these functions.

The two solutions from here Is there a way to use Blueprint Function Libraries or an equivelent feature for non-actor blueprints? - #4 by UnrealEverything should work for you as well, unfortunately they both require some C++ code.

Unfortunately doesn’t work in a child class of Object.

Thanks for the tip, but a few years ago when I tried C++ I was prevented from launching my project with only a generic “Try rebuilding from source manually” message and I swore I’d never be locked out like that again, and since I stopped using C++ I never have.

The workarounds are truly a struggle, though…