UE C++ 设置碰撞前 后事件 碰撞中事件

一.在Actor中声明碰撞BOX组件

cpp 复制代码
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
		class UBoxComponent* MyBox;

在Actor以这样的形式实现代理绑定,在BeginPlay()里。

cpp 复制代码
MyBox->OnComponentBeginOverlap.AddDynamic();

转到OnComponentBeginOverlap定义

再转到这个代理的定义

cpp 复制代码
/** Delegate for notification of start of overlap with a specific component */
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_SixParams( FComponentBeginOverlapSignature, UPrimitiveComponent, OnComponentBeginOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult);

这里是6个参数,从后向前数六个参数。将中间的","去掉,作为调用函数的参数。其余两个绑定也是类似的思路。

cpp 复制代码
	UFUNCTION()
	void BeginOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	UFUNCTION()
	void EndOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); //四个参数
	// FComponentEndOverlapSignature, UPrimitiveComponent, OnComponentEndOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex
	UFUNCTION()
	void HitOverlaoFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
	//UPrimitiveComponent*, HitComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, FVector, NormalImpulse, const FHitResult&, Hit

声明后在cpp中实现

cpp 复制代码
void AMyActor::BeginOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("BeginOverLapEvent is Success"));
}

void AMyActor::EndOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("EndOverlapEvent is Success"));
}

void AMyActor::HitOverlaoFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("HitOverlapEvent is Success"));
}

回调的函数完成

最上面的代理绑定完整版如下:

cpp 复制代码
	MyBox->OnComponentBeginOverlap.AddDynamic(this,&AMyActor::BeginOverlapFunction);
	MyBox->OnComponentEndOverlap.AddDynamic(this,&AMyActor::EndOverlapFunction);
	MyBox->OnComponentHit.AddDynamic(this,&AMyActor::HitOverlaoFunction);

一般这里有报错,有可能就是代理调用函数的参数个数弄错了。

这里测试碰撞前 碰撞后的代理函数

设置碰撞,就可以一直调用 碰撞中的代理函数

相关推荐
眠りたいです13 分钟前
基于脚手架微服务的视频点播系统-数据管理与网络通信部分的预备工作
c++·qt·ui·微服务·云原生·架构·媒体
烦躁的大鼻嘎16 分钟前
【Linux】深入Linux多线程架构与高性能编程
linux·运维·服务器·开发语言·c++·ubuntu
野生的编程萌新19 分钟前
【C++深学日志】C++编程利器:缺省参数、函数重载、引用详解
c语言·开发语言·c++
愚润求学19 分钟前
【贪心算法】day10
c++·算法·leetcode·贪心算法
Slaughter信仰21 分钟前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十三章知识点问答(15题)
java·开发语言·jvm
智者知已应修善业43 分钟前
【矩阵找最大小所在位置】2022-11-13
c语言·c++·经验分享·笔记·算法·矩阵
小柯J桑_1 小时前
C++之特殊类设计
java·开发语言·c++
QiZhang | UESTC1 小时前
JAVA算法练习题day11
java·开发语言·python·算法·hot100
bigdata-rookie1 小时前
Java 反射
java·开发语言
能工智人小辰1 小时前
Java8 Swing实现计算器
开发语言