이벤트와 델리게이트(Delegate) 방식의 통지(Notifications)가 클래스 간 결합도(Grouping)을 최소화 하는 방식으로 발행될 수 있도록 보장하는 데 유용하며 임의의 클래스가 통지를 받을 수 있게 등록하는 것을 허용한다.

 

  • 가상함수로 구현된 이벤트 핸들링

언리얼에서 제공되는 일부 액터와 컴포넌트 클래스에는 가상 함수의 형태로 이벤트 핸들러가 포함돼 있다.

이 레시피에서는 문제의 가상 함수를 재정의해 핸들러를 사용자 정의하는 방법을 보여줄 것이다.

 

Hadder

더보기


#pragma once

#include "CoreMinimal.h"
#include "Components/BoxComponent.h"
#include "GameFramework/Actor.h"
#include "MyTriggetrVolume.generated.h"

UCLASS()
class WILLIAM_API AMyTriggetrVolume : public AActor
{
GENERATED_BODY()



//UProperty
public:
UPROPERTY()
UBoxComponent* TriggerZone;

//UFUNCTION
public:

UFUNCTION()
virtual  void NotifyActorBeginOverlap(AActor* OtherActor) override;
UFUNCTION()
virtual  void NotifyActorEndOverlap(AActor* OtherActor) override;

public:
AMyTriggetrVolume();

protected:
virtual void BeginPlay() override;

public:
virtual void Tick(float DeltaTime) override;

};

 

Cpp

더보기

 

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyTriggetrVolume.h"


// Sets default values
AMyTriggetrVolume::AMyTriggetrVolume()
{
  // 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;

TriggerZone = CreateDefaultSubobject<UBoxComponent>("TriggetZone");
TriggerZone->SetBoxExtent(FVector(200, 200, 10));
}

// Called when the game starts or when spawned
void AMyTriggetrVolume::BeginPlay()
{
Super::BeginPlay();
}

// Called every frame
void AMyTriggetrVolume::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

void AMyTriggetrVolume::NotifyActorBeginOverlap(AActor* OtherActor)
{
Super::NotifyActorBeginOverlap(OtherActor);
GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, FString::Printf(TEXT("%s Entered Me"), *(OtherActor->GetName())));
}

void AMyTriggetrVolume::NotifyActorEndOverlap(AActor* OtherActor)
{
Super::NotifyActorEndOverlap(OtherActor);
GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, FString::Printf(TEXT("%s Left Me"), *(OtherActor->GetName())));
}

 

 

실행화면이다.

아주 간단해서 주석과 해석은 달지 않는다.

+ Recent posts