[C++] Is there a way to get input from actor that isn a pawn/character?

Is there a way to get input from actor that isn a pawn/character ?
Like you have a component wich acts as your input but is just an component and not an pawn/character?

i need it for c++ not blueprint.

https://answers.unrealengine.com/questions/166084/check-keyboard-events-in-code.html

EnableInput(this);

I realize this is a very old question and it still doesn’t have an answer, so I am going to answer with a question. Did you find a way?

Here is my solution to this problem - basically you make your own function to setup the bindings. This example has two action mappings in Project Settings - One called ZoomIn and one called ZoomOut. All it does at the moment is write a message the Output log window when each event occurs but you can expand it to any number of bindings and put what you like in the delegate functions.
The header file

UCLASS()
class CUSTOMBP01_API AInput : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AInput();
	//two functions to be called when a key is pressed
	void ZoomIn();
	void ZoomOut();
	//a boolean to be set/reset just to record the last key event state
	bool bZoomingIn;
	/** Component that handles input for this actor, if input is enabled. */
	//UPROPERTY()
	//	class UInputComponent* InputComponent;  //already defined in scope AActor

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	// Called to bind functionality to input
	void SetupMyPlayerInputComponent(UInputComponent* myInputComponent);
	//take away 'class' identifier used in unreal pawnwithcamera example
	//change name to eliminate warning message :-
	
};

The cpp file:-

// Fill out your copyright notice in the Description page of Project Settings.

#include "customBP01.h"
#include "Input.h"
//#include "KeyPressing.h"   //no such file or directory

//General Log used only for display of messages to Output Log
DEFINE_LOG_CATEGORY(YourLog);

// Sets default values
AInput::AInput()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AInput::BeginPlay()
{
	//Run the BeginPlay function of this class' parent class - do I need this
	//What does it actually do.
	Super::BeginPlay();
	//the most recently enabled actor will be at top of input stack
	//see unreal docs on input for details of stack priority
	//enableinput takes a player controller 
	EnableInput(GetWorld()->GetFirstPlayerController());
	//the enable input used here means i dont have to enable auto receive input in the editor window
	UInputComponent* myInputComp = this->InputComponent;  //InputComponent variable is from AActor.h
	//check that it is valid before calling
	//i.e. not null.  
	if (myInputComp)
	{
		SetupMyPlayerInputComponent(myInputComp);
	}
	
}

// Called every frame
void AInput::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	//"MyCharacter's Bool is %s"
	//UE_LOG(YourLog, Warning, TEXT("AInput bZoomingIn is %s"), (this->bZoomingIn ? TEXT("True") : TEXT("False")));
	//UE_LOG(YourLog, Warning, TEXT("hasbindings is %s"), (this->InputComponent->HasBindings() ? TEXT("True") : TEXT("False")));

	if (bZoomingIn)
	{
		//UE_LOG(YourLog, Warning, TEXT("true really true"));
	}
	else
	{
		//UE_LOG(YourLog, Warning, TEXT("false really false"));
	}





}

void AInput::ZoomIn()
{
	UE_LOG(YourLog, Warning, TEXT("zoomin called"));
	bZoomingIn = true;
}

void AInput::ZoomOut()
{
	UE_LOG(YourLog, Warning, TEXT("zoomout called"));
	bZoomingIn = false;
}



//finally got this being called when enabled auto receive input.
void AInput::SetupMyPlayerInputComponent( UInputComponent* myInputComponent) {
	UE_LOG(YourLog, Warning, TEXT("SETUP PLAYER INPUT COMPONENT"));
	//Super::SetupPlayerInputComponent(InputComponent);   //Another experimental thing 

	//Hook up events for "ZoomIn" and "ZoomOut"
	myInputComponent->BindAction("ZoomIn", IE_Pressed, this, &AInput::ZoomIn);
	myInputComponent->BindAction("ZoomOut", IE_Released, this, &AInput::ZoomOut);

}

I have put in a lot of comments so you should be able to work out what is going on.

hope this helps.

1 Like

I have just posted an answer to this. Maybe it will help you.

Thanks for spending the time.

Oh my god ! You’re my hero just right now. I spent the entire day trying to do that correctly.

Thanks you! I have had same problems. I’m gonna try this

Thanks man! this saved me a lot of trouble!