Can I process input within UGameInstance?
I want to handle a keypress within my custom UGameInstance. I'm not sure how to go about doing this or if it is possible.
asked Jan 08 '19 at 10:22 AM in C++ Programming
I don't think that's possible. Or, at least, there is no built-in way to enable player input on that object.
The technical reason is, player input is enabled by the UInputComponent class. Being a component, the UInputComponent can only be added to actors (classes deriving from AActor, for example APawn). For a detailed explanation on the use of components, check out this article in the official docs: Components
Since UGameInstance object is not an actor (it doesn't derive from AActor), it cannot accept components like the UInputComponent. Therefore, it can't handle player input.
Typically, player input is expected to be handled on either your Pawn class or your Player Controller class. (Further info on the docs: PlayerInput (Input Component)) This makes sense, because player input is usually meant to cause some action to occur from the player's perspective in the game. In other words, player inputs cause player actions.
By contrast, the UGameInstance object represents the whole game session, at a higher level than even game modes or individual maps/levels. It's almost like the game executable itself. From this context, how would player input be interpreted? For a game with local multiplayer, which player's input is supposed to do what? For dedicated servers, which have a Game Instance but no players until multiplayer clients connect, how should player input work? Since the UGameInstance object exists before gameplay even starts (and perhaps even before the main menu is loaded), what should player input be used for in that case? See what I mean? Player input doesn't make much sense from the context of the Game Instance object.
Hopefully that explains why using UGameInstance for player input is likely not possible — or, at least, is certainly not a good idea.
Is there something special you're trying to accomplish that makes you not want to handle the player input in your Pawn or Player Controller classes?
answered Jan 08 '19 at 05:37 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here