UE4基础-触发重叠事件 | Word count: 373 | Reading time: 1min | Post View:
代码实现 方式一:
通过重写继承来的NotifyActorBeginOverlap
,这更易于实现
1 2 3 4 5 6 7 8 Public: virtual void NotifyActorBeginOverlap (AActor* OtherActor) ;
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 public : virtual void NotifyActorBeginOverlap (AActor* OtherActor) override ; void AFPSObjectiveActor::NotifyActorBeginOverlap (AActor* OtherActor) { Super::NotifyActorBeginOverlap (OtherActor); AFPSCharacter* Mycharacter = Cast<AFPSCharacter>(OtherActor); if (Mycharacter) { Destroy (); } }
方式二:
通过组件触发event:Comp->OnComponentBeginOverlap.AddDynamic(this, &AClass::Handlefunc);
其中处理函数自己创建,函数类型见下方举例部分。
光标放在OnComponentBeginOverlap
上,按F12:
1 2 UPROPERTY (BlueprintAssignable, Category="Collision" )FComponentBeginOverlapSignature OnComponentBeginOverlap;
光标放在FComponentBeginOverlapSignature
上,按F12(Visual Studio,Go to Definition):
这里声明了事件本身,相关的处理函数要对应后六个 参数,将它们复制,变量名称和变量类型之间的逗号。
1 2 DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_SixParams ( FComponentBeginOverlapSignature, UPrimitiveComponent, OnComponentBeginOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool , bFromSweep, const FHitResult &, SweepResult);
举例:
//绑定这类型的函数时,需将其标记为UFUNCTION ,以便让虚幻后端明白该函数的含义及如何将其与事件相绑定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 UPROPERTY (VisibleAnywhere, Category = "Components" ) UBoxComponent* OverlapComp; UFUNCTION () void HandleOverlap (UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) ; OverlapComp = CreateDefaultSubobject<UBoxComponent>(TEXT ("OverlapComp" )); OverlapComp->OnComponentBeginOverlap.AddDynamic (this , &AFPSExtractionZone::HandleOverlap); void AFPSExtractionZone::HandleOverlap (UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { }