x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.20
Tags:
more ▼

asked Jan 08 '19 at 10:22 AM in C++ Programming

avatar image

jonnybolton42
12 4 4 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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?

more ▼

answered Jan 08 '19 at 05:37 PM

avatar image

the_batch
1.2k 4 5 8

avatar image Shadowriver Jan 08 '19 at 07:28 PM

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

avatar image the_batch Jan 08 '19 at 08:05 PM

Good to know, @Shadowriver! 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!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question