I have a scene component, i want to add an action input to it, i use it but it do not work. Any one help me please
this is my code
What do you mean, “It does not work.” You have several log statements there. Are any of them printing anything?
There are at least a couple of possible problems with your code.
- If the actor-owner of this component spawns before your character, then it won’t bind.
- If there are more than one actors in your scene that have this component, only the last one that calls the bind action will be bound. You can’t bind the same action to the same Input component.
Given your lack of clarity on what you are trying to do, I’m going to assume this MouseGrabber is a component on some actor in your scene that you want to react when the mouse button is clicked.
First piece of advice, switch to the Gameplay Ability System and let it do all the work for you!
But if not then…
Since you want this to be attached an actor in the world that is not the player-character, then it will be possible to have multiple actors that have this component. Therefore you should NOT be binding this to the PlayerController. Instead, you should be binding it to the InputComponent on the actor that owns the component. And then add that component to the stack on the Player controller.
Now, when the left button is pressed, it will be processed, and OPTIONALLY consumed, by each input component in the stack in the following order:
- Actors (in order of latest enabled)
- PlayerControllers
- LevelScripts
- Pawns
However, as soon as any one of those consumes the input, the rest of the stack will never hear about the input.
So to properly set this up with all that in mind, you should first setup up the Binding and add it to the player controller input stack. But then immediately disable it, then using a collision box around your actor, to enable and disable the binding as the player enter and leaves the box respectively.
Also, best practice is to use and action names function names for what the action will do, not what binding key you are pressing. Grab
not LeftMouse Click
and OnLeftClickPressed
. In theory, you can allow your users to change the keys or what about controllers or touch. My wife is left handed her “primary button” is the right mouse button, not the left.
For example:
//MouseGrabber.h
const FInputActionBinding& GrabBinding;
//MouseGrabber.cpp
void UMouseGrabber::BeginPlay()
{
Super::BeginPlay();
BindToInput();
//...
}
void UMouseGrabber::BindToInput()
{
// Get the owner
AActor* ComponentOwner = GetOwner();
ComponentOwner->InputComponent = NewObject<UInputComponent>(this);
ComponentOwner->InputComponent->RegisterComponent();
if (ComponentOwner->InputComponent)
{
// Bind inputs here
GrabBinding = ComponentOwner->InputComponent->BindAction("Grab", IE_Pressed, this, &UMouseGrabber::Grab);
// etc...
// Now hook up our InputComponent to one in a Player
// Controller, so that input flows down to us
ComponentOwner->EnableInput(GetWorld()->GetFirstPlayerController());
ConsumeInputBinding(GrabBinding, false);
}
}
void ConsumeInputBinding( const FInputActionBinding& Binding, bool bShouldConsume )
{
Binding.bConsumeInput = bShouldConsume
}
void UMouseGrabber::Grab()
{
ConsumeInputBinding(GrabBinding, false);
//...
}
First, thanks for your help
It does like what you say, that i want to make a component that attached an actor is not player-character, and there will be multiple actors that have the component.
Currently i use UE5.1 which i just found the old input system is deprecated. Could you show me how to bind the “Grab” action in the new system
It could be a silly question but i’m new to this system, so thanks for your help at all
Everything is here: Enhanced Input
in your code i wonder what is GrabBinding because the EnableInput function return a void?
I fixed the code. Sorry bout that.
Thank you so much
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.