gpt4 book ai didi

c++ - 在 UE 4.18 中为 AActor 添加 OnClicked 监听器

转载 作者:行者123 更新时间:2023-11-28 04:47:51 26 4
gpt4 key购买 nike

我正在尝试向 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

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

最佳答案

你的 BeginPlay 方法有错误

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

error C3867: 'AInterestItem1::moveToItem': non-standard syntax; use '&' to create a pointer to member

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

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

error C2672: 'TBaseDynamicMulticastDelegate::__Internal_AddDynamic': no matching overloaded function found

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

希望对您有所帮助:)

关于c++ - 在 UE 4.18 中为 AActor 添加 OnClicked 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48895565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com