Download

Accessing a customly created blueprint GameInstance in code?

The use-case I’m looking for: So I have an image in my UI, which is representing my “inventory” which can hold only one item. When picking up something to the inventory you will drop the previous item and pick up the new item (the image in the UI is a representation of your picked up item). When I load the game map level, I add my UI widget as a member variable to my game instance and then add it to the viewport. So if I could access my custom GameInstance class from C++, I could easily access the member variable widget, and then the widgets image and change the brush to represent the image of the new item.

I’ve thought of using


AActor::GetGameInstance

and casting it to my custom class, however, I haven’t found a way to cast to a Blueprint class, is that even possible?

You simply cast to your C++ class;
Then if you want to access any UProperty that only exist in the Blueprint child class while the C++ doesn’t know that property… You’ll have to use Code Reflection with TFieldIterator.

Reflection is both good and bad, depends on what and how you use it.
I don’t know if there’s tutorials on it since EpicGames usually don’t want you to use Reflection.

[USER=“434”]BrUnO XaVIeR[/USER] That’s the problem, the GameInstance class is directly created as a Blueprint class from the default GameInstance, I haven’t created a C++ class for it. Will I have to do it?

You cannot access Blueprints in C++ directly.
Create it in C++.

@Raildex_ Thanks for clearing that up!