【问题标题】:Add OnClicked listener to AActor in UE 4.18在 UE 4.18 中将 OnClicked 监听器添加到 AActor
【发布时间】:2018-07-31 10:09:49
【问题描述】:

我正在尝试向演员添加事件侦听器,以便在游戏中点击演员时执行某些任务。我已经尝试了多种方法,包括 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

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

【问题讨论】:

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


    【解决方案1】:

    你的 BeginPlay 方法有错误

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

    错误 C3867:“AInterestItem1::moveToItem”:非标准语法;使用 '&' 创建指向成员的指针

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

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

    错误 C2672:“TBaseDynamicMulticastDelegate::__Internal_AddDynamic”:找不到匹配的重载函数

    此错误还表明,没有接受您传递的参数的 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

    希望对你有帮助:)

    【讨论】:

    • 谢谢,代表的链接看起来很有帮助。我已经在网上查看了许多示例,这就是我获得代码的地方,但所有示例都产生了错误 C2672: 'TBaseDynamicMulticastDelegate::__Internal_AddDynamic': 找不到匹配的重载函数,尝试时出错。有没有可以链接到您知道有效的具体示例?
    • 我已经编辑了答案,我使用的UE版本是4.18.3。
    • 是的!!!我刚刚找到了这个答案。希望更多的 UE 问题即将到来。 UE4论坛真的很烂
    • @Hani 谢谢,我可以打赌它可以使用它进行编译,但仍然没有调用该函数(我没有看到日志打印输出。是否可能有项目设置或其他东西需要改吗?
    • @PaulB 您是否在编辑器或游戏模式中启用了点击事件?尝试在 moveToItem 函数中使用断点调试项目,以检查是否收到点击事件。另外,检查此链接Logging in UE4 并尝试将日志级别更改为写入控制台的内容。记住要在正确的地方寻找日志,因为不同的级别主要意味着将日志写入不同的地方。
    猜你喜欢
    • 2014-06-17
    • 2013-01-12
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多