포스트

UE5 C++ | Obstacle Assault, Tick과 Speed, DeltaTime (유데미)

언리얼 강의 중 Section 3를 정리한 세번째 글입니다.

UE5 C++ | Obstacle Assault, Tick과 Speed, DeltaTime (유데미)

강의 정보 🎓


Tick 🔂

Game Loop

  1. 플레이어 인풋 처리
  2. 게임 상태 업데이트 (물리, 오브젝트 위치 등)
  3. 게임 상태 렌더링
    • 이 과정을 통해 출력된 하나의 장면 = 프레임
    • 초당 프레임 수 = 프레임 레이트 (FPS)
    • Tick 함수는 매 프레임마다 호출되어 게임 로직 업데이트를 담당하게 된다.

Tick 함수 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Called when the game starts or when spawned
void AMovingPlatform::BeginPlay()
{
    Super::BeginPlay();
    SetActorScale3D(FVector(5.0, 5.0f, 0.5f)); // 초기 스케일 설정
}

// Called every frame
void AMovingPlatform::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    MyVector.Y = MyVector.Y + 1;
    SetActorLocation(MyVector);
}

로컬 변수 📌

  • C++에서 로컬 변수는 함수 내에서만 살아있음 (스코프 기준)
  • 아래 예시는 LocalVector.Z만 변화하고 실제 위치는 반영되지 않음
1
2
3
4
5
6
7
8
9
10
11
// Called every frame
void AMovingPlatform::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    FVector LocalVector = MyVector;
    LocalVector.Z += 1; // 이 값은 외부에 영향을 주지 않음

    MyVector.Y = MyVector.Y + 1;
    SetActorLocation(MyVector); // 여전히 MyVector 기준으로 이동
}

의사 코드 💡

1
2
3
4
5
6
7
	// Move platform forwards
		// Get current location
		// Add vector to that location
		// Set the location
	// Send platform back if gone too far
		// Check how far we've moved
		// Reverse direction of motion if gone too far
  • 주석으로 작성된 의사 코드는 코드의 논리를 설명하는 데 유용함
  • 컴파일에는 영향을 주지 않음

함수 반환 값 ↩️

Expression (값을 생성)

  • 예시: MyVector, LocalVector.Z + 100, GetActorLocation()

Statement (동작 수행)

  • 예시: SetActorLocation(...), int32 A = 5;

나는 BeginPlay에서 시작 위치를 지정했지만 강의에서는 에디터로 직접 수정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Called every frame
void AMovingPlatform::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	// Move platform forwards
		// Get current location
	FVector CurrentLocation = GetActorLocation();
		// Add vector to that location
	CurrentLocation.X += 1;
		// Set the location
	SetActorLocation(CurrentLocation);
	// Send platform back if gone too far
		// Check how far we've moved
		// Reverse direction of motion if gone too far
}

속도& 델타타임 ⏱️

  • 초당 100cm 이동시키고 싶다면?
  • DeltaTime = 각 프레임이 걸린 시간
  • 프레임 수가 달라도 동일한 이동 속도를 유지할 수 있음
1
2
3
float Speed = 100.0f; // cm per second
FVector Offset = FVector(Speed * DeltaTime, 0, 0);
AddActorWorldOffset(Offset);

요약 및 느낀점 📝

  • Tick은 프레임마다 불려지는 함수로 게임 상태에 대해 다룬다.
  • 의사 코드를 통해 코드에 대해 설명하거나 논리적 생각을 정리 할 수 있다.
  • 로컬 변수, 함수 반환 값 등을 배우고 DeltaTime을 통한 이동도 구현하였다.
  • 시간의 개념을 통해 프레임 저하로 인하 차이를 방지 할 수 있다.

    느낀점 💡 tick, delta time을 통한 게임 상태를 관리 할 수 있게 되었으니 이를 활용하여 다양한 이동과 같은 것들을 구현 할 수 있어졌다.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.