プログラマーの卵の日記

備忘録になってます!小難しそうなことから超簡単なことまでやってます!!

【UE4】UE4C++を勉強し始めてみた

公式リファレンス:https://docs.unrealengine.com/ja/Programming/Introduction/index.html
参考:https://qiita.com/bigengelt/items/b17545fffe7b8d69e5e8


構成


No.1 準備
No.2 正直な話
No.3 コード







    No.1 準備


 C++用のプロジェクト作成は端折るとして、プロジェクト内のコンテンツブラウザのC++クラス内で、親クラスActorのものを適当に作って準備します。

f:id:yutateno:20200229191304p:plain








    No.2 正直な話


 公式リファレンス:https://docs.unrealengine.com/ja/Programming/Introduction/index.html を見たらおおよそ分かると思います。

公式にも書いてあったり書いてなかったりした内容について後半で書いていこうかと思っております。





    No.3 cppとhのコード


 公式のやつを見ながらその通りにやったらこんな感じになると思います。

.h


// 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 CTEST24_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;

	virtual void PostInitProperties() override;

	virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
		int32 TotalDamage;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
		float DamageTimeInSeconds;

	UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Transient, Category = "Damage")
		float DamagePerSecond;


private:
	void CalculateValues();

};



cpp


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


#include "MyActor.h"

// Sets default values
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;

	TotalDamage = 200;
	DamageTimeInSeconds = 1.f;
}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}

void AMyActor::PostInitProperties()
{
	Super::PostInitProperties();
	CalculateValues();
}


void AMyActor::CalculateValues()
{
	DamagePerSecond = TotalDamage / DamageTimeInSeconds;
}


#if WITH_EDITOR
void AMyActor::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
	CalculateValues();

	Super::PostEditChangeProperty(PropertyChangedEvent);
}
#endif



 出来たらMyActorのブループリントクラスを作成してその中にちゃんと出来ているか確認します。

f:id:yutateno:20200229191320p:plain

 

f:id:yutateno:20200229191442p:plain





 ヒトツヒトツの関数についてはある程度公式に書いてあると思いますが、一応簡単に言うと


UPROPERTY

 というのはマクロで、それの後に宣言した変数のカテゴリなどを設定します。細かいものについてまとめてくださっている方がいました。
参考:https://qiita.com/bigengelt/items/b17545fffe7b8d69e5e8

 そのマクロについて、例えばそのマクロを消してこのようにしたらカテゴリから消えました。


	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
		int32 TotalDamage;

	//UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
		float DamageTimeInSeconds;

f:id:yutateno:20200229191455p:plain





 他については公式ページとかでさも当たり前のようにPostEditChangePropertyなどを使い始めたのでどこかで一度関数一覧を見る必要があるように思いました。
どこにまとまってるか知らないけど