(Unreal : Tutorial) 4. 변수, 타이머, 이벤트

Posted by : at

Category : Unreal



UCLASS()
class CAMERATUTORIAL_API ACountdown : public AActor
{
	// ...

	int CountdownTime;
	UTextRenderComponent* CountdownText;
	void UpdateTimerDisplay();

    // Timer
	void AdvancedTimer();
	void CountdownHasFinished();
	FTimerHandle CountdownTimerHandle;
};
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("Countdown Text"));
	CountdownText->SetHorizontalAlignment(EHTA_Center);
	CountdownText->SetWorldSize(150.0f);
	RootComponent = CountdownText;

	CountdownTime = 3;
}

void ACountdown::UpdateTimerDisplay()
{
	CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));
}

void ACountdown::AdvancedTimer()
{
	--CountdownTime;
	UpdateTimerDisplay();

	if (CountdownTime < 1)
	{
		GetWorldTimerManager().ClearTimer(CountdownTimerHandle);
		CountdownHasFinished();
	}
}

void ACountdown::CountdownHasFinished()
{
	CountdownText->SetText(TEXT("Go!"));
}

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

	UpdateTimerDisplay();
	GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &ACountdown::AdvancedTimer, 1.0f, true);
}

// ...

변수 Unreal에서도 컨트롤 가능하게 만들기

UCLASS()
class CAMERATUTORIAL_API ACountdown : public AActor
{
	// ...

    // 여기 주석을 적으면 언리얼에서도 이 주석이 보임.
    UPROPERTY(EditAnywhere)
	int CountdownTime;
	
    // ...

블루 프린트에서 함수가 보이게 변경

UCLASS()
class CAMERATUTORIAL_API ACountdown : public AActor
{
	// ...

    UFUNCTION(BlueprintNativeEvent)
	void CountdownHasFinished();
    virtual void CountdownHasFished_Implementation();

	// ...
void ACountdown::CountdownHasFished_Implementation()
{
	CountdownText->SetText(TEXT("Go!"));
}

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include <Engine/Classes/Components/StaticMeshComponent.h>
#include "Door.generated.h"

UCLASS()
class TUTORIALPROJECT_API ADoor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ADoor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(EditAnywhere)
	UStaticMeshComponent* DoorMesh;

	UPROPERTY(EditAnywhere)
	float CloseTime;

	float DoorDeltaTime;

	bool bOpen;

	FTimerHandle DorTimerHandle;

	FRotator OriginRotation;

	void Open();
	void Close();
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "Door.h"

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


	DoorMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Door Mesh"));
	RootComponent = DoorMesh;

	CloseTime = 3.0f;
}

// Called when the game starts or when spawned
void ADoor::BeginPlay()
{
	Super::BeginPlay();
	
	OriginRotation = GetActorRotation();
	GetWorldTimerManager().SetTimer(DorTimerHandle, this, &ADoor::Open, 0.03f, true);
}

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

	DoorDeltaTime += DeltaTime;
}

void ADoor::Open()
{
	if (!bOpen)
	{
		bOpen = true;
		DoorDeltaTime = 0.0f;
	}

	FRotator rotation = GetActorRotation();
	rotation = OriginRotation + FRotator(0.0f, FMath::Lerp(0.0f, 90.0f, DoorDeltaTime), 0.0f);
	SetActorRotation(rotation);
	if (DoorDeltaTime > 1.0f)
	{
		GetWorldTimerManager().ClearTimer(DorTimerHandle);
		GetWorldTimerManager().SetTimer(DorTimerHandle, this, &ADoor::Close, 0.03f, true, CloseTime);
	}
}

void ADoor::Close()
{
	if (bOpen)
	{
		bOpen = false;
		DoorDeltaTime = 0.0f;
	}

	FRotator rotation = GetActorRotation();
	rotation = OriginRotation + FRotator(0.0f, FMath::Lerp(90.0f, 00.0f, DoorDeltaTime), 0.0f);
	SetActorRotation(rotation);
	if (DoorDeltaTime > 1.0f)
	{
		GetWorldTimerManager().ClearTimer(DorTimerHandle);
	}
}

About Taehyung Kim

안녕하세요? 8년차 현업 C++ 개발자 김태형이라고 합니다. 😁 C/C++을 사랑하며 다양한 사람과의 협업을 즐깁니다. ☕ 꾸준한 자기개발을 미덕이라 생각하며 노력중이며, 제가 얻은 지식을 홈페이지에 정리 중입니다. 좀 더 상세한 제 이력서 혹은 Private 프로젝트 접근 권한을 원하신다면 메일주세요. 😎

Star
Useful Links