본문 바로가기
Unreal Engine4 or 5/코드

언리얼 C++ 튜토리얼 003.CountDown Timer

by 눈야옹 2016. 4. 25.
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
57
58
59
// Sets default values
ACountDown::ACountDown()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = false;
 
    CountDownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountDownNumber"));
    CountDownText->SetHorizontalAlignment(EHTA_Center);
    CountDownText->SetWorldRotation(FRotator(0.0f, 180.0f, 0.0f));
    CountDownText->SetWorldSize(150.0f);
    RootComponent = CountDownText;
 
    CountDownTime = 3;
 
}
 
// Called when the game starts or when spawned
void ACountDown::BeginPlay()
{
    Super::BeginPlay();
    
    UpdateTimerDisplay();
    GetWorldTimerManager().SetTimer(CountDownHandle, 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)
    {
        // 카운트 다운이 완료 되었으니 타이머를 중지 시킨다.
        GetWorldTimerManager().ClearTimer(CountDownHandle);
 
        CountDownFinshed();
    }
 
}
 
void ACountDown::CountDownFinshed_Implementation()
{
    CountDownText->SetText(TEXT("GO!"));
}
 
 
cs

 

 

 

추가적인 TimerManager Input 이벤트 구현

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
57
58
59
60
/** 카운트 다운이 끝났다면 다시 실행한다. 실행중이라면 작동하지 않는다.*/
    UFUNCTION(BlueprintCallable, Category = "CountDown")
    void CountDownReset(int Num);
 
    /** 카운트 다운을 일시 정지한다.*/
    UFUNCTION(BlueprintCallable, Category = "CountDown")
    void CountDownPause();
 
    /** 카운트 다운을 다시 시작한다.(단 핸들링이 아직 존재해야한다.)*/
    UFUNCTION(BlueprintCallable, Category = "CountDown")
    void CountDownPlay();
 
    /** 숫자를 원하는것을 넣어준다.*/
    UFUNCTION(BlueprintCallable, Category = "CountDown")
    void CountDownSetTime(int Num);f, true);
 
 
void ACountDown::CountDownReset(int Num = 3)
{
    
    GEngine->AddOnScreenDebugMessage(-10.5f, FColor::Blue, "CountDownReset");
 
    if (GetWorldTimerManager().IsTimerActive(CountDownHandle))
    {
        //활성화 중이라면
    }
    else
    {
        //활성화중이 아니라면
        CountDownTime = Num;
        GetWorldTimerManager().SetTimer(CountDownHandle, this, &ACountDown::AdvanceTimer, 1.0f, true);
    }
}
 
void ACountDown::CountDownPause()
{
    GEngine->AddOnScreenDebugMessage(-10.5f, FColor::Blue, "CountDownPause");
 
    GetWorldTimerManager().PauseTimer(CountDownHandle);
}
 
void ACountDown::CountDownPlay()
{
    GEngine->AddOnScreenDebugMessage(-10.5f, FColor::Blue, "CountDownPlay");
 
    GetWorldTimerManager().UnPauseTimer(CountDownHandle);
}
 
void ACountDown::CountDownSetTime(int Num)
{
    if (GetWorldTimerManager().IsTimerActive(CountDownHandle))
    {
        //활성화 중이라면
        CountDownTime = Num;
    }
    else
    {
        //활성화중이 아니라면
    }
}
cs