How to register to receive input events from dynamically added components?

I am dynamically adding an actor component to the player when they venture into a certain range with an exterior system (game mode).

On the actor component itself I want to put input events.

If the component is on the player by default, the events work, if the component is dynamically added during runtime, the events don’t work.

I am assuming I am missing something when I add the component to register to receive inputs? How do I do this? Am I doing something that should not be done? I want to do it this way so I can isolate certain things from each other. Seems like I’m trying to enable input on an actor component or something.

On the actor component itself I want to put input events.

Actor components can’t process input. Generally speaking, you do want to handle input in as few places as possible. Pawn, Controller or a focused Widget are good places to do so.

What I’d do - I’d implement the action input in the pawn the components are added to. Pawns can dynamically look-up run-time added components:

And equip the base class component with an Interface:

The functionality of Pause, Destroy, Use can now be implemented on the base class component level and / or extended by its children.

  • we detect a key press in the Pawn => a generic Use message is sent to the component(s)
  • if a component implements that functionality, they can process it in their own unique way
  • if no implementation is present, nothing happens; that’s the beauty of interfaces - graceful fails

Since this is a combination of both interfaces and inheritance, each actor component child can completely override the entire behavior. So you can mash the same key and get very different behaviors from every component. It does not matter here whether they are added dynamically.

2 Likes

I like this solution. Thanks for this!

I would like to add just for info. It is weird that you can add input events to an ActorComponent to process.

I did some digging into the engine code and depending on when the Actor Component is created, the events can get binded. It looks like in this case they only get binded on ActorCreation assuming the actor already has the component.

I tried binding manually in c++, didn’t seem to work though. Solution seems overcomplicated at this point and probably will change to your suggestion.

2 Likes