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.

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?

It is possible, InputComponent is not one grabbing the input there few stages to it, but you would need to replicate what InputCompoennt is doing, or you could hook up to viewport or even direcly to InputDevice. Note that this is C++ and you can essentially do anything you like, but in most cases there really no point

Also UEngine is the main class which you could consider “game executable itself”… which you can also override btw ;] but you need to do that in ini file

Good to know, @anonymous_user_f5a50610! Though I think the important part is that following sentence, which we seem to agree about: in most cases, there’s really no need to modify things that way, or that much.

Oh, and no argument from me about the UEngine class, of course. I just couldn’t think of a better analogy for the high-level nature of the game instance object. My point was, simply, there are better places to put one’s player input code, and usually those places are the pawn and player controller classes.

Anyway, thanks for adding to the learning!