U++ 增强输入

来自泡泡学习笔记
跳到导航 跳到搜索
// Fill out your copyright notice in the Description page of Project Settings.


#include "IAPawnTest.h"




// Sets default values
AIAPawnTest::AIAPawnTest()
{
 	// Set this pawn 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 AIAPawnTest::BeginPlay()
{
	Super::BeginPlay();


	// 在你的cpp中...
	if (APlayerController* PC = Cast<APlayerController>(GetController()))
	{
		if (UEnhancedInputLocalPlayerSubsystem* InputSystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
		{
			InputSystem->AddMappingContext(InputMapping, 0);
		}
	}
	
}

// Called every frame
void AIAPawnTest::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void AIAPawnTest::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	UEnhancedInputComponent* Input = Cast<UEnhancedInputComponent>(PlayerInputComponent);
	// 你可以通过更改"ETriggerEvent"枚举值,绑定到此处的任意触发器事件
	Input->BindAction(IA_ATTACK, ETriggerEvent::Triggered, this, &AIAPawnTest::FUNC_ATTACK);
	
}

void AIAPawnTest::FUNC_ATTACK(const FInputActionValue& Value)
{

	// 获取此处所需任意类型的输入动作的值...
	FVector VectorValue = Value.Get<FVector>();
	FVector2D AxisValue2D = Value.Get<FVector2D>();
	float FloatValue = Value.Get<float>();
	bool BoolValue = Value.Get<bool>();

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, BoolValue ? TEXT("true") : TEXT("false"));

	// 在此处实现你的精彩功能!
}


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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"


#include "InputActionValue.h"
#include "EnhancedInputSubsystems.h"
#include "GameFramework/PlayerController.h"
#include "EnhancedInputComponent.h"

#include "IAPawnTest.generated.h"


UCLASS()
class PRACTICE_API AIAPawnTest : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	AIAPawnTest();

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
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	
	void FUNC_ATTACK(const FInputActionValue& Value);

	// 将映射上下文公开为头文件中的属性...
	UPROPERTY(EditAnywhere, Category = "Input")
	class UInputMappingContext* InputMapping;

	UPROPERTY(EditAnywhere, Category = "Input")
	class UInputAction* IA_ATTACK;


};