[UE4]OnComponentBeginOverlap.AddDynamic 的编译错误

以 Character 类为例,假设有 PacManCharacter 派生自 Character类
首先在 PacManCharacter.h 头文件中添加碰撞函数的声明: 

OnCollision 为自定义的碰撞函数,名称可以任意,但参数形式必须满足以下条件

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

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

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

碰撞函数的实现:

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 支持的格式,编译会报参数类型不匹配的错误

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 应该对碰撞函数进行如下声明和定义:

UFUNCTION()  
void OnCollision(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);  
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

转载于:https://www.cnblogs.com/timy/p/8645492.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值