InputTouch doesnt work

Hello. I want to make a vehicle turn on pressing left/right half of a screen. Ive made thet in BP and it works perfect, but I cant make it in code. Here it is. It compiles but doesnt work. Can you please tell me what is wrong? Even first log function doesnt work.

bool AMyWheeledVehicle::InputTouch(FViewport* InViewport,
	uint32 Handle,
	ETouchType::Type Type,
	const FVector2D & TouchLocation,
	FDateTime DeviceTimestamp,
	uint32 TouchpadIndex)
{
	UE_LOG(LogTemp, Warning, TEXT("Input Touch Kinda Works"));
	if (TouchpadIndex == 1)
	{
		switch (Type)
		{
			case ETouchType::Began:
			{
				FVector2D ViewportSize = FVector2D(GEngine->GameViewport->Viewport->GetSizeXY());
				UE_LOG(LogTemp, Warning, TEXT("Touch detected"));
				if (TouchLocation.X < ViewportSize.X / 2)
				{
					GetVehicleMovementComponent()->SetSteeringInput(-1);
				}
				else
				{
					GetVehicleMovementComponent()->SetSteeringInput(1);
				}
				break;
			}

			case ETouchType::Ended:
			{
				GetVehicleMovementComponent()->SetSteeringInput(0);
				break;
			}
		}
		return true;
	}
}

End here is the blueprint

Hello Vanja,

Did you define your function in the input of the project ?
Did you setup the function in the SetupInput / SetupInputComponent ?

Also, with what you say and if the first log is not call, does the function is actually call?

Because after a quick look, your function seem OK, but i didn’t test it.

gamer08

Thank you very much for your response. The problem is I didnt know how to do it. There are no guides about touch input, so i had to do everything myself using minimal amount of info given in unreal documentation. Would you please be so kind to explain me how should i do it (Define function in input and set it up?) I have prototyped it in header and then defined it in cpp file of my wheeled vehicle class. Should i make a new touch interface and define it there? I thought defining and prototyping it in wheeled vehicle class was enough just as for all the others functions and like in BP. Thank you in advance

Hi Vanja, I will assume 4 things tell me if i’m wrong.

You use the 4.8 engine version as mark in the question.

You Create a c++ project and not blueprint based.

You Create a project based on the Vehicule template.

You Create a project aimed for mobile/Tablet and not for PC/Console.

I need these infos to point you in the right direction.

Other though: InputTouch exists on PlayerController so you can override it if you want. Anyway I will wait your answers because I don’t want to confuse you.

gamer08

  1. I use unreal 4.8.3.
  2. Project was created as blueprint based, then ive made new c++ based project and nothing has changed.
  3. I have created empty project and wrote code for a vehicle using wheeled vehicle template as a reference.
  4. Project is aimed for Mobile/Tablet.

Ok, you have a couple of options for what you want to do.

You can manage input on PlayerController (The class that control your car class). On playerController you can intercep input “earlier”, to do more stuff.

You can manage input on car directly.

Based on what you asked, i’m gonna go with the second choice, manage input directly on vehicule, but you have multiples options.

Option 1 input in car directly

You need to ovveride function to handle input on your car directly

header file

virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;

Choice 1

Source file

void AVehiculeMobilePawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	// set up gameplay key bindings
	check(InputComponent);

    if(FPlatformMisc::GetUseVirtualJoysticks() || GetDefault<UInputSettings>()->bUseMouseForTouch )
	{
            InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &&AVehiculeMobilePawn::BeginTouch);
		InputComponent->BindTouch(EInputEvent::IE_Released, this, &&AVehiculeMobilePawn::EndTouch);
	}

}

After you create these two functions on your car, Begin and End. In these functions you can use the code you written on your first post.

Choice 2 (I’m based on c++ Vehicule template aimed for mobile)

Source file

void AVehiculeMobilePawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	// set up gameplay key bindings
	check(InputComponent);

	InputComponent->BindAxis("MoveForward", this, &AVehiculeMobilePawn::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &AVehiculeMobilePawn::MoveRight);

    /*More binds ....*/
}

You Create your 2 functions moveForward (not required in example) MoveRight. In the moveRight you can use your code as above.

If you follow option 2 you’ll need to define these fuctions in projects setting input.

Compile and lauch your game. In Unreal Engine, you go Edit–>Project Settings–> Engine section on left–>Input.

It will be axis mappings you can refer at this if you want to : Input doc and axis and actions mapping for more details

Option 2 input on PlayerController

If you want to intercept input earlier and have more controls you can manage input on you PlayerController.

PlayerController.h

virtual bool InputTouch(uint32 Handle, ETouchType::Type Type, const FVector2D& TouchLocation, FDateTime DeviceTimestamp, uint32 TouchpadIndex);

Look more similar to what you have.

If need more details on that option, just tell me i’m gonna explain too.

If you have any problems or questions don’t hesitate to ask, i’m glad to help.

gamer08

Thank you very much for your help! Here is how i have made it if smb faces similar problem.

//APlayerVehicle.h

protected:
	virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;

	void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location);

	void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location);

//APlayerVehicle.cpp

void APlayerVehicle::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	UE_LOG(LogTemp, Warning, TEXT("Touch Input Works"));
	if (FPlatformMisc::GetUseVirtualJoysticks() || GetDefault<UInputSettings>()->bUseMouseForTouch)
	{
		InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &APlayerVehicle::BeginTouch);
		InputComponent->BindTouch(EInputEvent::IE_Released, this, &APlayerVehicle::EndTouch);
	}
}

void APlayerVehicle::BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
{
	FVector2D ViewportSize = FVector2D(GEngine->GameViewport->Viewport->GetSizeXY());
	UE_LOG(LogTemp, Warning, TEXT("Touch Began"));
	if (Location.X < ViewportSize.X / 2)
	{
		UE_LOG(LogTemp, Warning, TEXT("Turning Left"));
		GetVehicleMovementComponent()->SetSteeringInput(-1);
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Turning Right"));
		GetVehicleMovementComponent()->SetSteeringInput(1);
	}
}

void APlayerVehicle::EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
{
	UE_LOG(LogTemp, Warning, TEXT("Touch Ended"));
	GetVehicleMovementComponent()->SetSteeringInput(0);
}