언리얼

[언리얼,C++] 언리얼 C++ 공부 정리(베르 - 함수)

Esunn 2022. 8. 6. 03:06

언리얼 프로그래밍에서 사용되는 모든 자료형은 반환형으로 사용 가능.

변수는 전 코드를 이용.

 

함수 선언, 구현

헤더파일에서 먼저 클래스에 새로 생성할 함수의 원형을 적어 알려줌(선언).

 

함수를 구현할 때는 클래스이름::함수이름을통해 구현해줌.

 

 

PostInitProperties() : 오브젝트의 변수가 초기화될 때 호출되는 함수.

PostEditChangeProperty() : 변수가 수정될 때 호출되는 함수.

 

초록색 밑줄이 그어진 함수 이름에 커서를 두고 Ctrl + . 단축키

정의 만들기 선택을 통해 빈 함수 구현을 에디터가 해줌.

 

언리얼 C++ Super 키워드 : 상속받은 부모 클래스에 있는 원본 프로퍼티나 함수를 가져오는 데 사용.

 

https://youtu.be/0KQzIK-TZ-U

여기 부분은 전체적으로 더 찾아보고 공부해봐야겠다.

C++ 자체에 대한 이해가 적은 것 같기도 하고 더 알아봐야겠다.

 

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


#include "MyActor01.h"

// Sets default values
AMyActor01::AMyActor01() : TotalDamage(400), DamageTimeInSeconds(2.5f)
{
 	// 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;

	UE_LOG(LogTemp, Log, TEXT("Constructor"));

	//DamagePerSecond = TotalDamage / DamageTimeInSeconds;
	//함수 영상에서 기본값 지정 X

	CharacterName = TEXT("Nickname");
	bAttackable = true;
}

// Called when the game starts or when spawned
void AMyActor01::BeginPlay()
{
	Super::BeginPlay();
	
	UE_LOG(LogTemp, Log, TEXT("BeginPlay"));
}

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

	//UE_LOG(LogTemp, Log, TEXT("Tick"));
}

void AMyActor01::CalculateDPS()
{
	DamagePerSecond = TotalDamage / DamageTimeInSeconds;
}

void AMyActor01::PostInitProperties()
{
	Super::PostInitProperties();
	CalculateDPS();
}

void AMyActor01::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
	CalculateDPS();
	Super::PostEditChangeProperty(PropertyChangedEvent);
}
//MyActor01.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

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

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
		int32 TotalDamage;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
		float DamageTimeInSeconds;
	UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Transient, Category = "Damage")
		float DamagePerSecond;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FString CharacterName;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		bool bAttackable;

	
	void CalculateDPS();

	virtual void PostInitProperties() override;
	virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;

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

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

};

 

실행을 해보면 디테일 패널에서 값을 바꿀 때마다 유동적으로 계산되어 값이 바뀐다.