How to get game instance in plugin?

Hi,

I can get world in my plugin using this:

GEditor->GetEditorWorldContext().World();

But when I use this below to get a game instance, it returns nullptr or other wrong value:

GEditor->GetEditorWorldContext().World()->GetGameInstance();

What should I do?

Thanks.

Plugin, game project or engine source code, it does not matter where you code module is it works the same anywhere, it’s only diffrent ways of distributing it, so you do the same way as you do in game project module.

GameInstance is instated only in play mode, it won’t exist in editor world, also if this code should work in package game you should not use GEditor or editor world which won’t exist in package game, you get compiler errors during packaging.

If you want farther help, tell what you need game instice for? If you looking for place to put some global code that persistently works in editor and practically entire engine, module class is good place, but problem with is that it outside of reflection system.

Thanks for your reply. I use a custom game instance to store some variables, and now I want to get them in my plugin. Is there any other way to do it?

What perpace of those varables are, are they for game or for editor… are they are related to editor in any way?

these variables are coordinates of a cube in game level… And I think they are no relation to editor?
Besides, If I play the level in editor viewport(PIE), it has no game instance? I can not understand what the “play mode” means…Do you mean “play in editor” or “standalone game”?

It has game instance. thing is you accessing editor world not PIE world which are seperate. try getting PlayerController first from GEngine:

And get world from here, if you get null that meansp layercontroller dont exist and it most likely editor world, Explore UEngine you might find some better way:

You need to make you code react properly for non-existance of game instance

And as i said since oyu said this is more for game use then editor, dont use editor APIs like GEditor for runtime code, or else game that use you plugin won’t be able to be packaged.

Sorry for reply late, do you mean
GEngine->GetFirstLocalPlayerController(GEngine->GetWorld()) ?

update:
Thanks a lot! I use these code to get a game instance finally:
TArray<APlayerController *> playerList;
GEngine->GetAllLocalPlayerControllers(playerList);
playerList[0]->GetWorld()->GetGameInstance();

Is there a better way to do it?

Most direct way to get the game instance from anywhere is:

((UGameEngine*)GEngine)->GameInstance

Why was this answer down voted? That’s the fastest way to get GameInstance. One thing - i’d better use cpp/ue4 cast, and pointers check of course.

Thats probably because there faster methods, all actors have GetGameInstance() with cast template, you can also get it from UWorld which is commonly available, you should use those before doing method above. But i will up vote this too ;]

Easy case: utility plugins often does not have an actor/uobject types at all so, no GameInstance / UWorld ptr cashed inside. Just a module singleton and standart cpp types, but they need an option to send data / call methods from GameInstance. So the first option you have: pass a GameInstance pointer to type constructor, and cache it locally, and the second: to use GEngine static pointer.

upd: will not work in editor. Editor has UUnreadEdEngine type, not UGameEngine.

I use the following:

((UGameEngine*)GEngine)->GameInstance

Before the “Red frames”, I can get the GameInstance and CustomGameInstance, but when I try to get a variable inside CustomGameInstance, I can get nothing, can anyone help me about this?