我正在尝试向 Actor 添加事件侦听器,以便在游戏中单击 Actor 时可以执行某些任务。我尝试了多种方法,包括OnClicked.Add,OnClicked.AddDynamic以及在其他答案中看到的每种方法的多个版本,但最终都没有编译并给出类似的错误。
这是我目前的类(class)
// Fill out your copyright notice in the Description page of Project
Settings.
#include "InterestItem1.h"
// Sets default values
AInterestItem1::AInterestItem1()
{
// 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 AInterestItem1::BeginPlay()
{
Super::BeginPlay();
OnClicked.AddDynamic(this, AInterestItem1::moveToItem);
}
// Called every frame
void AInterestItem1::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AInterestItem1::moveToItem(UPrimitiveComponent* ClickedComp, FKey
ButtonPressed){
UE_LOG(LogTemp, Log, TEXT("meh, they clicked me"));
}
与此有关,我在VS中收到以下错误
没有函数模板“FActorOnClickedSignature::__ Internal_AddDynamic”的实例与参数列表匹配
并在输出日志中出现此编译错误
CompilerResultsLog: Error: C:\Users\AtLink\Documents\Unreal Projects\Axis_Test_Purged\Source\Axis_Test_Purged\InterestItem1.cpp(18) : error C3867: 'AInterestItem1::moveToItem': non-standard syntax; use '&' to create a pointer to member
CompilerResultsLog: Error: C:\Users\AtLink\Documents\Unreal Projects\Axis_Test_Purged\Source\Axis_Test_Purged\InterestItem1.cpp(18) : error C2672: 'TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,AActor *,FKey>::__Internal_AddDynamic': no matching overloaded function found
CompilerResultsLog: ERROR: UBT ERROR: Failed to produce item: C:\Users\AtLink\Documents\Unreal Projects\Axis_Test_Purged\Binaries\Win64\UE4Editor-Axis_Test_Purged-4805.dll
我是虚幻的新手,所以我可以对自己做错的事情做一些简单的解释。谢谢。
最佳答案
您的BeginPlay方法中出现错误
OnClicked.AddDynamic(this, AInterestItem1::moveToItem);
您应该创建一个指向member的指针,并将其传递给AddDynamic,如下所示:
OnClicked.AddDynamic(this, &AInterestItem1::moveToItem);
此错误还表明,没有使用您传递的参数的AddDynamic函数。
网络上有很多示例,例如:
AddDynamic example
我在自己的项目中也使用了此功能:
// Sets default values
AArea::AArea()
{
// 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;
USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = SphereComponent;
this->StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
this->StaticMeshComponent->SetupAttachment(RootComponent);
OnClicked.AddDynamic(this, &AArea::SelectArea);
}
void AArea::SelectArea(AActor* TouchedActor, FKey ButtonPressed) {
PlayerController->ChangeSelectedArea(this);
}
请确保在此处阅读有关委托(delegate)的文档:
Unreal Engine Delegates
希望能帮助到你 :)