본문 바로가기

Unreal

[Unreal] UPROPERTY, UFUNCTION (BlueprintNativeEvent)

UPROPERTY

프로퍼티 지정자는 BlueprintReadWrite, VisibleAnywhere, EditAnywhere를 주로 사용 합니다.

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyCat")
int value = 0;

 

아래는 언리얼 프로퍼티 관련 사이트 입니다.

docs.unrealengine.com/ko/ProgrammingAndScripting/GameplayArchitecture/Properties/index.html

 

프로퍼티

게임플레이 클래스에 대한 프로퍼티 생성 및 구현 관련 레퍼런스입니다.

docs.unrealengine.com

 

UFUNCTION 

함수 지정자는 BlueprintCallable, BlueprintImplementableEvent, BlueprintNativeEvent 를 주로 사용 합니다.

UFUNCTION(BlueprintCallable, Category = "MyWebSocket")
void ConnectServer(FString serverURL, FString serverProtocol);

UFUNCTION(BlueprintNativeEvent)
void OnReceived(const FString& param);
virtual FString OnReceived_Implementation(FString param);

 

아래는 언리얼 펑션 관련 사이트 입니다.

docs.unrealengine.com/ko/ProgrammingAndScripting/GameplayArchitecture/Functions/index.html

 

UFunction

게임플레이 클래스용 함수 생성 및 구현 레퍼런스입니다.

docs.unrealengine.com

 

연습한 .h .cpp 파일

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include <Components/TextRenderComponent.h>
#include "Countdown.generated.h"

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

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

public:	

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category="Test")
	int32 CountdownTime;

	UTextRenderComponent* CountdownText;
	void UpdateTimerDisplay();
	void AdvanceTimer();

	UFUNCTION(BlueprintNativeEvent, Category="Test")
	void CountdownHasFinished();
	virtual void CountdownHasFinished_Implementation();
    
    UFUNCTION(BlueprintCallable, Category="Test")
	TArray<FString> GetFiles();

	FTimerHandle CountdownTimerHandle;
};

 

#include "Countdown.h"

// Sets default values
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("CntTxt"));
	CountdownText->SetHorizontalAlignment(EHorizTextAligment::EHTA_Center);
	CountdownText->SetWorldSize(150.0f);
	RootComponent = CountdownText;

	CountdownTime = 3;
}

// Called when the game starts or when spawned
void ACountdown::BeginPlay()
{
	Super::BeginPlay();
	
	UpdateTimerDisplay();
	GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &ACountdown::AdvanceTimer, 1.0f, true);
}

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

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

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

void ACountdown::CountdownHasFinished_Implementation()
{
	CountdownText->SetText(TEXT("START"));
}

TArray<FString> AMyFirstActor::GetFiles()
{
	TArray<FString> Result;

	FString dir = FPaths::ProjectSavedDir();
	FString fileExt = TEXT("*");
	IFileManager::Get().FindFiles(Result, *dir, *fileExt);

	//std::string test2 = std::string(TCHAR_TO_UTF8(*Result[0]));
	UE_LOG(LogTemp, Warning, TEXT("%s"), *Result[0]);

	return Result;
}

 

참조 유투브

www.youtube.com/watch?v=Uw7Svlh1gF0&t=302s