How to call BindAction on an object of another class instead of `this`

I have made a function in the Pistol class (An APawn) named APistol::Shoot()

void APistol::Shoot()
{ /*Shoot*/ }

And in the SetupPlayerInputComponent function, I have done this:

Super::SetupPlayerInputComponent(PlayerInputComponent);

	if (InputComponent)
	{
		InputComponent->BindAction("Shoot", IE_Pressed, /*here*/, &APistol::Shoot);
	}

I want to call this function in such a way that the player pawn (the pawn who is holding this pistol) gives the input, and then the pistol starts shooting…
Probably I need to replace /*here*/ with a reference to the player pawn. But it doesn’t seem to work. There is another way, I can somehow do all this work in the player pawn’s class itself. But that would end up making the code messy. Sorry if it is common knowledge in c++ :sweat_smile:,and any help would be appreciated.

The issue here is that &APistol::Shoot would be a call to the static class, not to the instance of the pistol. It wouldn’t work anyway.

I’d say create a function in the player class, which will call the pistol function.
.h:

UFUNCTION()
void ShootPressed();

.cpp:

InputComponent->BindAction("Shoot", IE_Pressed, this, &ThisClass::ShootPressed);
...
void ShootPressed()
{
    Pistol->Shoot();
}
3 Likes

I tried google/gpt the same question but found no answer. Just found this out from Nicholas Quinn’s comment in the Tom Looman Course.

// I think yours would look like this
InputComponent->BindAction("Shoot", IE_Pressed, this->Pistol.Get(), &APistol::Shoot);
// assuming Pistol is spawned or created in your Character.

// For reference this is how mine looks
EnhancedInputComp->BindAction(Input_Interact, ETriggerEvent::Triggered, this->InteractionComp.Get(), &USInteractionComp::PrimaryInteract);

It worked in PIE. I’m using Enhanced Input and TObjectPtr for the InteractionComp, but guys from the course said it works with the old Input Component and with raw pointers.

Sidenote, make sure APistol::Shoot() is not const.