在UE 4.18中将OnClicked侦听器添加到AActor

时间:2018-02-20 22:57:07

标签: c++ visual-studio-2017 unreal-engine4

我正在尝试向actor添加一个事件侦听器,以便在游戏中单击actor时可以执行某些任务。我尝试了很多东西,包括OnClicked.Add,OnClicked.AddDynamic以及我在其他答案中看到的每个版本的多个版本,但它们最终都没有编译并给出类似的错误。

这是我目前的课程

// 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

我是虚幻的新手,所以我可以用一些简单的解释来解释我做错了什么。感谢。

1 个答案:

答案 0 :(得分:1)

您的BeginPlay方法在行

中有错误
OnClicked.AddDynamic(this, AInterestItem1::moveToItem);
  

错误C3867:&#39; AInterestItem1 :: moveToItem&#39 ;:非标准语法;使用&#39;&amp;&#39;创建指向成员的指针

您应该创建一个指向成员的指针并将其传递给AddDynamic,如下所示:

OnClicked.AddDynamic(this, &AInterestItem1::moveToItem);
  

错误C2672:&#39; TBaseDynamicMulticastDelegate :: __ Internal_AddDynamic&#39;:找不到匹配的重载函数

此错误还表明,没有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);
}

请务必在此处阅读有关代表的文档:

Unreal Engine Delegates

希望有所帮助:)