Access from C++ to variable of GameInstance

Hi.
I created a GameInstance via blueprint called “MyGameInstance”. Into it there`s a public variable.
I want to access to that variable from my c++ actors class.
When Im trying to type:
UMyGameInstance the compiler tells me that is not defined.
I wish I could UMyGameInstance*** MGI = Cast(GetGameInstance())*
and get that variable, or call a function to get it.

Into defaultengine.ini: GameInstanceClass=/Game/Blueprints/MyGameInstance.MyGameInstance_C
because its set on project setup > maps and modes > game instance class

So i cannot get a reference of my game instance from c++. Any idea?

Thanks!!

1 Like

first i think you should #include “yourgameinstance.h” file where you wanna use gameinstance.

Second you can get gameinstance from ugameplaystatics with getworld or playercontroller or whatever :slight_smile:

YourGameInstanceClass* GI = Cast<YourGameInstanceClass>(UGameplayStatics::GetGameInstance(GetWorld()));
	if (GI)
	{
		// Do Something
	}
4 Likes

The simplest method in have found for doing this is to implement a overridden function that retrieves the value from the blueprint. Implement a native function that is overridden on the blueprint to retrieve the value. The same can be done with setting the values from c++. There are other methods but for the sale of simplicity this is the way we perform reverse communication between c++ and blueprints.

Otherwise simply creating a d derived fame instance in c++ implementing the variable then using that class is probably the second simplest way depending on where you are at on your development procesd.