Can you BindAction to a const function?

Hello friends!

I’m trying to create a Player Pawn that binds the Click action of the mouse to a function called SelectAtClick. I’ve been taught that if a method is not altering its own class, to mark it as const. With SelectAtClick, all the method does is perform a raycast from mouse position to see what’s been hit so, based off this guideline, it should be marked as const. However, if I try to mark SelectAtClick as const, the BindAction does not work and I get the following error.


C2664 'FInputActionBinding &UInputComponent::BindAction<APlayerPawn>(const FName,const EInputEvent,UserClass *,void (__cdecl APlayerPawn::* )(FKey))':
cannot convert argument 4 from 'void (__cdecl APlayerPawn::* )(void) const' to 'void (__cdecl APlayerPawn::* )(void)'

My code for binding the Click action:


PlayerInputComponent->BindAction("Click", IE_Pressed, this, &APlayerPawn::SelectAtClick);

My method declaration:


virtual void SelectAtClick() const;

I’ve done some searching online, but haven’t found a way to get around this error. It’s not essential that I mark it const, but it would keep my coding consistent. (During my refactoring, I came across it and thought “Why didn’t I const this?”)

Any insight anyone can provide is greatly appreciated!

Thanks!

While in principle your approach makes sense, it looks to me like you are passing a pointer to a function (void SelectAtClick()) through a mechanism (BindAction()) that is designed to contemplate scenarios where the pointer points to a function that might actually alter its class.

Because there isn’t a version of BindAction() that accepts a pointer to a const function (based on my quick look at the API reference), I think you will need to deviate from your principle in this instance.

Just one person’s opinion…