Download

Conversion from GameInstance to CustomGameInstance fails.

Hello guys, a friend of mine as an issue on Unreal Engine 4. We’re looking for some help around.

We created a plugin to get files (like pictures) from a local disk or internet. For this plugin to work, upon launching the UE4 project, the custom GameInstance need to create a variable (Based on UObject) from the plugin. This variable will be used to scan the local disk and get a list of the files it contains. The plugin will then use this list to check upon the files. Hence it needs to get the variable created inside the custom GameInstance.

We tried several time in several mamner, but when the GameInstance convert into Custom GameInstance, it fails.

Did this happen to anyone before? Any insights are welcome!

Please find below extract of the code:

To answer your question.

1.) You have to make sure the CustomGameInstance class is set to be class to used for a level.
2.) You can use UGameplayStatics to get the game instance and cast it, like this:



UCustomGameInstance* MyGameInstance = Cast<UCustomGameInstance>(UGameplayStatics::GetGameInstance(GetWorld()));


One additional note.

People aren’t likely going to enjoy having to inherit from a game instance to use your plugin. Instead you may want to look at Subsystems. There’s even a GameInstanceSubsystem that will automatically be created/destroyed with the game instance - without you having to provide a custom version.

https://docs.unrealengine.com/en-US/…ems/index.html

Then when you want to access your subsystem, you simply call.



UFMS* FileSystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<UFMS>();