CTimeOfDayHandler.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CTimeOfDayHandler.generated.h"
//멀티캐스트 델리게이트 파라미터(시그니쳐함수이름,파라미터1,파라미터2)
DECLARE_MULTICAST_DELEGATE_TwoParams(FOnItmeChangedStignature,int32,int32)
UCLASS()
class WILLIAM_API ACTimeOfDayHandler : public AActor
{
GENERATED_BODY()
public:
//시그니쳐로 델리게이트를 만든다.
FOnItmeChangedStignature OnTimeChanged;
UPROPERTY()
int32 TimeScale;
UPROPERTY()
int32 Hours;
UPROPERTY()
int32 Minutes;
UPROPERTY()
float ElapsedSeconds;
public:
ACTimeOfDayHandler();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
};
CTimeOfDayHandler.cpp
#include "CTimeOfDayHandler.h"
ACTimeOfDayHandler::ACTimeOfDayHandler()
{
PrimaryActorTick.bCanEverTick = true;
//프로퍼티 초기화
TimeScale = 60;
Hours = 0;
Minutes = 0;
ElapsedSeconds = 0;
}
void ACTimeOfDayHandler::BeginPlay()
{
Super::BeginPlay();
}
void ACTimeOfDayHandler::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
ElapsedSeconds += (DeltaTime*TimeScale);
if(ElapsedSeconds>60)
{
ElapsedSeconds -= 60;
Minutes++;
if(Minutes>60)
{
Minutes -= 60;
Hours++;
}
}
OnTimeChanged.Broadcast(Hours, Minutes);
}
ACClock.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CClock.generated.h"
UCLASS()
class WILLIAM_API ACClock : public AActor
{
GENERATED_BODY()
public:
UPROPERTY()
USceneComponent* RootSceneComponent;
UPROPERTY()
UStaticMeshComponent* ClockFace;
UPROPERTY()
USceneComponent* HourHandle;
UPROPERTY()
UStaticMeshComponent* HourHand;
UPROPERTY()
USceneComponent* MinuteHandle;
UPROPERTY()
UStaticMeshComponent* MinuteHand;
UFUNCTION()
void TimeChanged(int32 Hours, int32 Minutes);
FDelegateHandle MyDelegateHandle;
public:
ACClock();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
};
ACClock.cpp
#include "CClock.h"
#include "Kismet/GameplayStatics.h"
#include "CTimeOfDayHandler.h"
ACClock::ACClock()
{
PrimaryActorTick.bCanEverTick = true;
RootSceneComponent = CreateDefaultSubobject<USceneComponent>("RootSceneComponent");
ClockFace = CreateDefaultSubobject<UStaticMeshComponent>("ClockFace");
HourHand = CreateDefaultSubobject<UStaticMeshComponent>("HourHand");
MinuteHand = CreateDefaultSubobject<UStaticMeshComponent>("MinuteHand");
HourHandle = CreateDefaultSubobject<USceneComponent>("HourHandle");
MinuteHandle = CreateDefaultSubobject<USceneComponent>("MinuteHandle");
auto meshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/02/Sphere1.Sphere1'"));
if(meshAsset.Object !=nullptr)
{
ClockFace->SetStaticMesh(meshAsset.Object);
HourHand->SetStaticMesh(meshAsset.Object);
MinuteHand->SetStaticMesh(meshAsset.Object);
}
RootComponent = RootSceneComponent;
HourHand->AttachTo(HourHandle);
MinuteHand->AttachTo(MinuteHandle);
HourHandle->AttachTo(RootSceneComponent);
MinuteHandle->AttachTo(RootSceneComponent);
ClockFace->AttachTo(RootSceneComponent);
ClockFace->SetRelativeTransform(FTransform(FRotator(90, 0, 0), FVector(10, 0, 25), FVector(2, 2, 0.1)));
HourHand->SetRelativeTransform(FTransform(FRotator(0, 0, 0), FVector(0, 0, 25), FVector(0.1, 0.1, 0.5)));
MinuteHand->SetRelativeTransform(FTransform(FRotator(0, 0, 0), FVector(0, 0, 50), FVector(0.1, 0.1, 1)));
}
void ACClock::BeginPlay()
{
Super::BeginPlay();
TArray<AActor*>TimeOfDayHandlers;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ACTimeOfDayHandler::StaticClass(), TimeOfDayHandlers);
if(TimeOfDayHandlers.Num()!=0)
{
auto TimeOfDayHandler = Cast< ACTimeOfDayHandler>(TimeOfDayHandlers[0]);
MyDelegateHandle = TimeOfDayHandler->OnTimeChanged.AddUObject(this, &ACClock::TimeChanged);
}
}
void ACClock::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ACClock::TimeChanged(int32 Hours, int32 Minutes)
{
HourHandle->SetRelativeRotation(FRotator(0, 0, 30 * Hours));
MinuteHandle->SetRelativeRotation(FRotator(0, 0, 6 * Minutes));
}
실행결과


'UnraealEngine' 카테고리의 다른 글
| 언리얼 엔진 5 멀티플레이어 가이드: 세션 관리 & Net Role/Authority 심층 (0) | 2025.03.13 |
|---|---|
| UE5 C++ 멀티플레이어: RPC & 리플리케이션 심층 가이드 (0) | 2025.03.13 |
| Unreal4 이벤트와 델리게이트 처리하기 5)사용자 정의 이벤트 생성 (0) | 2021.08.24 |
| Unreal4 이벤트와 델리게이트 처리하기 4)멀티캐스트 델리게이트 생성 (0) | 2021.08.24 |
| Unreal4 이벤트와 델리게이트 처리하기 3)입력 파라미터를 사용하는 델리게이트 생성 (0) | 2021.08.24 |