Best way to bind input to component?

Hi there guys.
Currently i’m trying to modularize all that i can via components for later use, and one of those things that i’m wanting to modularize is the ability to interact with others “interactable” objects.

Right now the component will iterate on every Overlaping actor on the Owner and check whether they implement the IInteractableInterface (if they do or not respond to the Interact message). All goes well until this point but the moment that i press the Interact key (“E” default), i need the InteractorComponent to call the Interact method on itself.

Now my question goes like this, what is the best way to bind the input to that component?
I have been thinking on this scenarios:

  1. The Actor Owner should bind the action manually on the input component via

    InputComponent->BindAction(“Interact”, IE_Pressed,
    this->InteractorComponentPointer, &UInteractorComponent::Interact);

But this breaks the advantage of being plug and play, also i would need to know that the Interact method exists on my component (knowing too much of the component).

  1. Pass the InputComponent and the Event name to the component and bind the action on the initialize component

    //On InitializeComponent on the interactor
    this->InputComponent->BindAction(this->InteractActionName…

I think that this would be good, i only would need to configure the component on creation and it would be set, but a drawback would be that it needs an input component and that breaks modularization a little

  1. I have read that delegates are the way to go but i don’t know how they could be applied here

Any suggestions?

Greetings?

Hello, I know the post is old, but I came across your doubt trying to do exactly the same. I was able to solve it as follows.

In the component header:

ACharacter* OwnerCharacter;

In the component source:

void UCharacterInteractionComponent::BeginPlay() {
	Super::BeginPlay();

    OwnerCharacter = Cast<ACharacter>(GetOwner());
    OwnerCharacter->InputComponent->BindAction("Interact", IE_Pressed, this, &UCharacterInteractionComponent::Interact);
}

Hi, is this still working in 4.18? InputComponent seems to be NULL on BeginPlay().

hmm, the InputComponent being NULL on BeginPlay sounds wrong, are you sure you are dealing with a possessed character/pawn and are not overwriting the InputComponent somehow yourself?

anyways, just tested it and it should be working in 4.18

perfectly working with blueprint pawn (4.20), thank you!

I also get null for the input component, the editor crashes on begin play. How did you solve it?

need some delay:

FTimerHandle TH_Delay;
const float DelayInSeconds = 2.f;
GetOwner()->GetWorldTimerManager().SetTimer(TH_Delay, this, &UHotkeys_Component::BindHotkeys, DelayInSeconds, false);