OnComponentBeginOverlap.AddDynamic 的编译错误【UE4】

xiaoxiao2021-02-28  89

最近在编写UE4代码的时候发现相关的API有变化,于是找了一下,发现这个博客,分享给大家

以 Character 类为例,假设有 PacManCharacter 派生自 Character类

首先在 PacManCharacter.h 头文件中添加碰撞函数的声明:

OnCollision 为自定义的碰撞函数,名称可以任意,但参数形式必须满足以下条件 [cpp] view plain copy 在CODE上查看代码片派生到我的代码片

UFUNCTION() void OnCollision(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

然后在 PacManCharacter.cpp 文件中的 SetupPlayerInputComponent 函数中添加绑定函数:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片

GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &PacManCharacter::OnCollision);

碰撞函数的实现:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片

void PacManCharacter::OnCollision(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {   GEngine->AddOnScreenDebugMessage(0, 1.0f, FColor::Red, TEXT("PacMan ! ")); }

但是以上是老版本的 UE4 支持的格式,编译会报参数类型不匹配的错误 [plain] view plain copy 在CODE上查看代码片派生到我的代码片

error C2664: 'void TBaseDynamicMulticastDelegate::__Internal_AddDynamic(UserClass *,void (__cdecl AItem::* )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &),FName)': cannot convert argument 2 from 'void (__cdecl AItem::* )(AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)' to 'void (__cdecl AItem::* )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)'

新版本的 UE4 应该对碰撞函数进行如下声明和定义:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片

UFUNCTION() void OnCollision(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片

void PacManCharacter::OnCollision(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {   GEngine->AddOnScreenDebugMessage(0, 1.0f, FColor::Red, TEXT("PacMan ! ")); }

即增加了第一个参数 class UPrimitiveComponent* HitComp 转载来自http://blog.csdn.net/panda1234lee/article/details/60584606

转载请注明原文地址: https://www.6miu.com/read-33161.html

最新回复(0)