[언리얼,C++] 언리얼 C++ 공부 정리(베르 - 1.변수, UPROPERTY)
변수, UPROPERTY
멤버 변수를 만들기 위해서는 클래스의 헤더 파일에서 선언해줘야 한다.
언리얼에서 변수를 쓰기 위해서는 UPROPERTY() 매크로를 사용해야 한다.
자료형 정리
정수
길이가 고정되어 있는 int8, int16, int32, int64 가 있다.
정수를 표현할 때 몇 비트를 쓸 지에 따라 숫자로 나타낸다.
C++에서 unsigned를 쓰듯이 변수 앞에 u를 붙여 양수 표현에만 쓸 수 있다.
uint8, uint16, uint32, uint64
실수
C++과 같이 float과 double이 있으며 각각 32비트, 64비트이다.
float은 소수점 5자리까지 정밀도를 double은 10자리까지 정밀도를 가짐.
문자열
언리얼 C++에서는 기본적으로 FString을 이용한다.
따옴표(" ")를 이용해서 넣는 것이 아닌 TEXT( )매크로를 이용해야 한다.
FString fstr = TEXT("text");
논리 변수
bool을 이용해줌.
전 시간 클래스를 다시 이용해서 예제 작성.
변수(함수)의 공개 범위
public : 클래스 외부의 모두에게 공개
protected : 클래스를 상속받은 자식 클래스에게만 공개
private : 외부 공개 X 클래스 내부에서만 이용
public로 설정해도 언리얼 에디터의 디테일 패널에서 공개되지는 않음.
변수 에디터에서 표시하기
이를 위해서는 UPROPERTY매크로를 이용
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
int32 TotalDamage;
EditAnywhere : 아키타입(archytype)과 레벨에 배치된 인스턴스 양 쪽 모두의 프로퍼티 창에서 편집 가능.
아키타입 : 인스턴스화 되지 않은 블루프린트의 원본
BlueprintReadWrite : 블루프린트에서 읽기 쓰기 모두 가능.
Category : 블루프린트 편지 툴이나 디테일 패널에서 카테고리로 묶음.
BlueprintReadOnly : 프로퍼티를 블루프린트에서 읽기만 가능.
VisibleAnywhere : 모든 프로퍼티 창에서 보이지만 편집X
Transient : 해당 프로퍼티가 휘발성으로 프로퍼티로 저장되지X
엑터를 배치하고 선택한 후 왼쪽 아래 디테일 패널을 보면 변수를 확인 가능하다.
변수의 값 설정
소스파일(cpp파일)로 가서 설정해준다.
생성자 옆에 콜론을 입력해 프로퍼티(값) 이런 식으로 설정하거나
생성자의 바디에서 일반적인 변수처럼 설정해주는 것이다.
//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;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
//MyActor01.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor01.h"
// Sets default values
AMyActor01::AMyActor01() : TotalDamage(10), DamageTimeInSeconds(1.2f)
{
// 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;
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"));
}
디테일 패널에서 설정한 변수의 값들을 확인 가능.