박하의 나날

[19] UE4 C++_tutorial 01

프로그래밍/Unreal

 

*Timer

타이머는 FTimerManager로 관리되고 어떤 델리게이트 유형도 할당시킬 수 있다.

 

CountDown.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once
 
#include "GameFramework/Actor.h"
#include "Countdown.generated.h"
 
UCLASS()
class UE4_TUTORIAL_API ACountdown : public AActor
{
    GENERATED_BODY()
    
public:    
    // Sets default values for this actor's properties
    ACountdown();
 
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
    
    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;
 
    UPROPERTY(EditAnywhere)
    int32 CountdownTime;
 
    UTextRenderComponent* CountdownText;
    void UpdateTimerDisplay();
 
    void AdvanceTimer();
 
    UFUNCTION(BlueprintNativeEvent)
    void CountdownHasFinished();
    virtual void CountdownHasFinished_Implementation();
    
    FTimerHandle CountdownTimerHandle;
};
 
cs

 

33)TimerHandle  카운트다운이 완료되면 Timer를 종료시킬 수 있도록 핸들을 유지해야한다.

 

CountDown.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "HowTo_VTE.h"
#include "Countdown.h"
 
// Sets default values
ACountdown::ACountdown()
{
    // Set this actor to call Tick() every frame. 
    PrimaryActorTick.bCanEverTick = false;
 
    CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
    CountdownText->SetHorizontalAlignment(EHTA_Center);
    CountdownText->SetWorldSize(150.0f);
    RootComponent = CountdownText;
 
    CountdownTime = 3;
}
 
// Called when the game starts or when spawned
void ACountdown::BeginPlay()
{
    Super::BeginPlay();
 
    UpdateTimerDisplay();
    GetWorldTimerManager().SetTimer(CountdownTimerHandle, this&ACountdown::AdvanceTimer, 1.0f, true);
}
 
// Called every frame
void ACountdown::Tick( float DeltaTime )
{
    Super::Tick( DeltaTime );
 
}
 
void ACountdown::UpdateTimerDisplay()
{
    CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));
}
 
void ACountdown::AdvanceTimer()
{
    --CountdownTime;
    UpdateTimerDisplay();
    if (CountdownTime < 1)
    {
        // We're done counting down, so stop running the timer.
        GetWorldTimerManager().ClearTimer(CountdownTimerHandle);
        //Perform any special actions we want to do when the timer ends.
        CountdownHasFinished();
    }
}
 
void ACountdown::CountdownHasFinished()
{
    //Change to a special readout
    CountdownText->SetText(TEXT("GO!"));
 
cs

 15) 카운트다운시간 초기화

34~37) 남은시간 표시하도록 텍스트 디스플레이 업데이트 혹은 시간다되면 0 표시.

ACountdown을 게임에 처음 스폰시킬 때 실행되며, CountdownTime변수가 0될때까지 초당 한번씩 실행된다.

46)카운트다운 완료되면 타이머 중지

48)타이머 종료시 특별 액션

 

1
2
3
GetWorldTimerManager().SetTimer(this&AMatineeActor::CheckPriorityRefresh, 1.0f, true);
 
GetWorldTimerManager().ClearTimer(this&AMatineeActor::CheckPriorityRefresh);
cs

1)타이머생성

3)타이머제거

 

 

'프로그래밍 > Unreal' 카테고리의 다른 글

[20] Object  (0) 2017.05.22
0521  (0) 2017.05.21
[18]UE4 C++_tutorial 0  (0) 2017.05.17
[17]액터  (0) 2017.05.15
[16] UE4 C++ 프로그래밍 입문_0  (0) 2017.05.15