Unreal Engine C++ 활용 프로그램 제작

 

세부 요구사항

  • 시작점은 (0, 50, 0) 에서 시작
  • 생성한 Actor C++ > Blueprint로 상속한 다음 Blueprint에서 StaticMesh추가 후 Cube 지정
    1. Turn, Move 함수를 만들고 함수를 호출하여 10회 랜덤으로 회전 및 이동
    2. 매번 이동시 현재 좌표를 출력
    3. 로그 출력은 AddOnScreenDebugMessage를 활용
  • 이동 시 마다 현재 좌표 및 이동 횟수 출력
  • 10회 이동 시 각 스텝마다, 50% 확률로 랜덤하게 이벤트 발생
  • 10회 이동 후에는 총 이동거리와 총 이벤트 발생 횟수 출력

구현

 

헤더파일

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

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

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

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

	void Turn();
	void Move();
	void RandomEvent();

private:
	FVector startLocation;
	FVector actorLocation;
	int turnCount;
	int moveCount;
};

 

Turn, Move 함수

void AMyActor::Turn()
{
	SetActorRotation(FRotator(100.f, 100.f, 100.f));
	actorLocation = GetActorLocation();

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(
			-1,
			10.0f,
			FColor::Yellow,
			*actorLocation.ToString()
		);
	}

}

void AMyActor::Move()
{
	AddActorLocalOffset(FVector(50.f, 0.f, 0.f));
	actorLocation = GetActorLocation();

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(
			-1,
			10.0f,
			FColor::Green,
			*actorLocation.ToString()
		);
	}
}

 

랜덤 출력 함수

void AMyActor::RandomEvent()
{
	int i_random = FMath::RandRange(0, 100);

	if (i_random > 50)
	{
		moveCount++;
		Move();
	}
	else
	{
		turnCount++;
		Turn();
	}
}

 

BeginPlay 함수 및 생성자

AMyActor::AMyActor()
{
 	// 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;
	turnCount = 0;
	moveCount = 0;
}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	SetActorLocation(FVector(0, 50, 0));
	startLocation = GetActorLocation();
	
	for (int i = 0; i < 10; i++)
	{
		RandomEvent();
		GEngine->AddOnScreenDebugMessage(
			-1,
			10.0f,
			FColor::Blue,
			FString::Printf(TEXT("Total Count : %d"), i+1)
		);

	}


	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(	// 회전 횟수 출력
			-1,
			10.0f,
			FColor::Blue,
			FString::Printf(TEXT("Turn Count : %d"), turnCount)
		);

		GEngine->AddOnScreenDebugMessage(	// 이동 횟수 출력
			-1,
			10.0f,
			FColor::Blue,
			FString::Printf(TEXT("Move Count : %d"), moveCount)
		);

		GEngine->AddOnScreenDebugMessage(	// 시작 지점에서 마지막 위치까지의 거리
			-1,
			10.0f,
			FColor::Red,
			*(actorLocation - startLocation).ToString()
		);
	}

}

+ Recent posts