![見出し画像](https://assets.st-note.com/production/uploads/images/70644186/rectangle_large_type_2_63eea1fc02cab8536a2f2a7780fe99d5.jpeg?width=1200)
UE5から始める C++ & BP 09 【C++版】Enumeration(列挙型)
Blueprint側が出来たし、あっという間に書けるだろう
C++のEnumerationは手ごわかった
C++は自由度があるので、BlueprintのEnumerationはかなり制限されていますね。
まだまだC++で出来ることは深そうです。
それでは、Blueprintの処理をC++で再現します。
【C++版】Enumeration(列挙型)
C++でBlueprintを再現すること
Enumeration(列挙型)をC++で作成します。
![](https://assets.st-note.com/img/1643032147700-DPi617CXXb.png?width=1200)
Switch文をint32から作成したEnumeration(列挙型)に変更します。
![](https://assets.st-note.com/img/1643032164783-uQ4iAC5Q7c.png?width=1200)
Visual Studioを開いて、編集するファイルを作成する
プロジェクトを閉じていたら、プロジェクトを開き、
「Chapter_2_Enumeration」を開きます。
![](https://assets.st-note.com/img/1643032172888-jqbVm7WnBS.png?width=1200)
[Tools]メニューから[New C++ Class]を開きます。
![](https://assets.st-note.com/img/1643032180287-lRgtEv4dfQ.png)
親クラスに[Actor]を選択します。
![](https://assets.st-note.com/img/1643032188124-7Rp12gMDZS.png?width=1200)
ClassTypeとClass名を設定します。
![](https://assets.st-note.com/img/1643032207255-aoQezzUedr.png)
![](https://assets.st-note.com/img/1643032213111-8nRCL8S8PI.png?width=1200)
Solution Explorerから今回編集する2つのファイルを開きます。
CPPFlowControlSwitchEnum.h
CPPFlowControlSwitchEnum.cpp
![](https://assets.st-note.com/img/1643032221050-RexdfBhgIE.png)
開いたファイルを学習する初期状態に修正します。
CPPFlowControlSwitchEnum.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "CPPFlowControlSwitchEnum.generated.h"
UCLASS()
class CPP_BP_API ACPPFlowControlSwitchEnum : public AActor
{
GENERATED_BODY()
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
// PrintString関数のDurationに設定する変数
const float Duration = 10.0f;
// PrintString関数のTextColorに設定する変数
const FLinearColor TextColor = FColor(255, 255, 255);
// 計算用の変数
int32 CalcVarA = 7;
int32 CalcVarB = 3;
// Flow Control用の変数
bool IsPrintHello = false;
int32 CalcType = 1;
};
CPPFlowControlSwitchEnum.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CPPFlowControlSwitchEnum.h"
#include "Kismet/KismetSystemLibrary.h"
// Called when the game starts or when spawned
void ACPPFlowControlSwitchEnum::BeginPlay()
{
Super::BeginPlay();
FString Message = "C++ Hello World!";
if (IsPrintHello)
{
// PrintStringノードと同じ処理
// UKismetSystemLibraryクラスのPrintString関数を呼び出す
UKismetSystemLibrary::PrintString(this, Message, true, true, TextColor, Duration);
}
else
{
switch (CalcType)
{
case 0:
{
// Add(足し算)の処理
int32 ResultAdd = CalcVarA + CalcVarB;
FString StrResultAdd = FString::Printf(TEXT("%d"), ResultAdd);
UKismetSystemLibrary::PrintString(this, StrResultAdd, true, true, FColor::Red, Duration);
break;
}
case 1:
{
// Subtract(引き算)の処理
int32 ResultSubtract = CalcVarA - CalcVarB;
FString StrResultSubtract = FString::Printf(TEXT("%d"), ResultSubtract);
UKismetSystemLibrary::PrintString(this, StrResultSubtract, true, true, FColor::Yellow, Duration);
break;
}
case 2:
{
// Multiply(掛け算)の処理
int32 ResultMultiply = CalcVarA * CalcVarB;
FString StrResultMultiply = FString::Printf(TEXT("%d"), ResultMultiply);
UKismetSystemLibrary::PrintString(this, StrResultMultiply, true, true, FColor::Green, Duration);
break;
}
default:
{
// Divide(割り算)の処理(int > float)
float ResultDivide = (float)CalcVarA / (float)CalcVarB;
FString StrResultDivide = FString::Printf(TEXT("%f"), ResultDivide);
UKismetSystemLibrary::PrintString(this, StrResultDivide, true, true, FColor::Blue, Duration);
}
}
}
}
C++でEnumeration(列挙型)「ECPPCalcType」を作成する
C++でEnumeration(列挙型)を作成します。
[Tools]メニューから[New C++ Class]を開きます。
![](https://assets.st-note.com/img/1643032238166-hpjc3WNTFi.png)
親クラスに「None」を選択します。
![](https://assets.st-note.com/img/1643032245925-MQYwxvzzYu.png?width=1200)
ClassTypeとClass名を設定します。
![](https://assets.st-note.com/img/1643032259102-AnVPYjmNRW.png)
![](https://assets.st-note.com/img/1643032266187-DC6Oy250Fu.png?width=1200)
「CPPCalcType.h」のみ使用するので、「CPPCalcType.cpp」は削除します。
Solution Explorerから「CPPCalcType.cpp」をRemoveします。
Solution ExplorerからRemoveしても、ファイルは残っているので、Explorerから「CPPCalcType.cpp」を削除します。
![](https://assets.st-note.com/img/1643032273487-dGePgbN58a.png?width=1200)
「CPPCalcType.h」にEnumeration(列挙型)を作成します。
![](https://assets.st-note.com/img/1643032280199-E2zhypN7Y2.png)
C++のEnumeration(列挙型)の書き方は以下のようになります。
CPPCalcType.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "CPPCalcType.generated.h"
UENUM(BlueprintType)
enum class ECPPCalcType : uint8
{
Add,
Subtract,
Multiply,
Divide
};
Solution Explorerから編集する2つのファイルを開きます。
CPPFlowControlSwitchEnum.h
CPPFlowControlSwitchEnum.cpp
![](https://assets.st-note.com/img/1643032293068-C506VFi1l1.png)
「CPPFlowControlSwitchEnum.h」を編集します。
include文に「CPPCalcType.h」を追加する
変数[CalcType]のVariableTypeを[int32]から[ECPPCalcType]に変更する。
変数[CalcType]の値をECPPCalcType::Subtractに設定する。
CPPFlowControlSwitchEnum.h
#include "CoreMinimal.h"
#include "CPPCalcType.h" // 追加
#include "CPPFlowControlSwitchEnum.generated.h"
int32 CalcType = 1;
↓
ECPPCalcType CalcType = ECPPCalcType::Subtract;
Enumeration(列挙型)を使用したswitch文の書き方は以下のようになります。
.cpp
switch (CalcType)
{
case ECPPCalcType::Add:
{
// Add(足し算)の処理
break;
}
case ECPPCalcType::Subtract:
{
// Subtract(引き算)の処理
break;
}
case ECPPCalcType::Multiply:
{
// Multiply(掛け算)の処理
break;
}
case ECPPCalcType::Divide:
{
// Divide(割り算)の処理(int > float)
}
}
「CPPFlowControlSwitchEnum.cpp」のswitch文を「ECPPCalcType」を使用した書き方に変更します。
CPPFlowControlSwitchEnum.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CPPFlowControlSwitchEnum.h"
#include "Kismet/KismetSystemLibrary.h"
// Called when the game starts or when spawned
void ACPPFlowControlSwitchEnum::BeginPlay()
{
Super::BeginPlay();
FString Message = "C++ Hello World!";
if (IsPrintHello)
{
// PrintStringノードと同じ処理
// UKismetSystemLibraryクラスのPrintString関数を呼び出す
UKismetSystemLibrary::PrintString(this, Message, true, true, TextColor, Duration);
}
else
{
switch (CalcType)
{
case ECPPCalcType::Add:
{
// Add(足し算)の処理
int32 ResultAdd = CalcVarA + CalcVarB;
FString StrResultAdd = FString::Printf(TEXT("%d"), ResultAdd);
UKismetSystemLibrary::PrintString(this, StrResultAdd, true, true, FColor::Red, Duration);
break;
}
case ECPPCalcType::Subtract:
{
// Subtract(引き算)の処理
int32 ResultSubtract = CalcVarA - CalcVarB;
FString StrResultSubtract = FString::Printf(TEXT("%d"), ResultSubtract);
UKismetSystemLibrary::PrintString(this, StrResultSubtract, true, true, FColor::Yellow, Duration);
break;
}
case ECPPCalcType::Multiply:
{
// Multiply(掛け算)の処理
int32 ResultMultiply = CalcVarA * CalcVarB;
FString StrResultMultiply = FString::Printf(TEXT("%d"), ResultMultiply);
UKismetSystemLibrary::PrintString(this, StrResultMultiply, true, true, FColor::Green, Duration);
break;
}
case ECPPCalcType::Divide:
{
// Divide(割り算)の処理(int > float)
float ResultDivide = (float)CalcVarA / (float)CalcVarB;
FString StrResultDivide = FString::Printf(TEXT("%f"), ResultDivide);
UKismetSystemLibrary::PrintString(this, StrResultDivide, true, true, FColor::Blue, Duration);
}
}
}
}
Ctrl + Sでファイルを保存し、Compileを行います。
![](https://assets.st-note.com/img/1643032308028-s90cG9WSPc.png?width=1200)
「CPPFlowControlSwitchEnum」をViewportにDrag&Dropします。
PrintStringの出力結果が分かりづらくなるので、「BP_FlowControl_SwitchEnum」を削除します。
![](https://assets.st-note.com/img/1643032315288-PcWxXhjpxt.png?width=1200)
Level Editorの[Play]ボタンをクリックします
![](https://assets.st-note.com/img/1643032324361-3Aqs8DjBFc.png)
変数[CalcType]の値が[ECPPCalcType::Subtract]なので、引き算の出力結果が表示されます。
![](https://assets.st-note.com/img/1643032332346-7FRXVg6Oiy.png)
C++で作成したEnumeration(列挙型)をBlueprintで使用する
C++で作成したEnumeration(列挙型)を使用できます。
C++で作成したEnumeration「ECPPCalcType」をBlueprintで使用します。
「BP_FLowControl_SwitchEnum」を開きます。
![](https://assets.st-note.com/img/1643032361384-mPMbazm67v.png)
VariableTypeにEnumeration「ECPPCalcType」を設定した変数[CPPCalcType]を変数に追加します。
![](https://assets.st-note.com/img/1643032353988-D94wtq5vPl.png)
![](https://assets.st-note.com/img/1643032370764-kp1DVAcNd9.png)
列挙型のDefaultValueですが、列挙定数が足りていない場合はプロジェクトを再起動してください。(UE5 EAしか起こらない?)
![](https://assets.st-note.com/img/1643032382274-jOnuoAquNo.png?width=1200)
追加した変数[CPPCalcType]でSwitchノードを作成してみます。
Bluerpintで作成したEnumeration[ECalcType]と同じOutputの実行ピンが表示されました。
![](https://assets.st-note.com/img/1643032387545-XdxSK73FER.png?width=1200)
C++で作成したEnumeration(列挙型)のDisplayNameを設定する
SwitchノードのOutputの実行ピンに列挙定数の名前が使われていました。
C++では列挙定数名とは別に「DisplayName」を設定してOutputの実行ピンの名称を変更できます。
CPPCalcType.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "CPPCalcType.generated.h"
UENUM(BlueprintType)
enum class ECPPCalcType : uint8
{
Add UMETA(DisplayName = "Addition"),
Subtract UMETA(DisplayName = "Subtraction"),
Multiply UMETA(DisplayName = "Multiplicatation"),
Divide UMETA(DisplayName = "Division"),
};
Ctrl + Sでファイルを保存し、Compileを行います。
![](https://assets.st-note.com/img/1643032406775-3kVSQrHOW1.png?width=1200)
「CPPCalcType.h」を変更してCompileすると、変数[CPPCalcType]に設定していたVariableTypeが「HOTRELOADED ECPPCalcType 0」というVariableTypeになってしまいます。
VariableTypeを[ECPPCalcType]に再設定します。
![](https://assets.st-note.com/img/1643032413848-31KHGmqU4v.png?width=1200)
Switchノードを再度作成すると、[Switch]ノードの実行ピンの名称がDisplay Nameで設定した名称に変更されています。
![](https://assets.st-note.com/img/1643032424531-wWi5u8y5S9.png?width=1200)
すべて保存してC++側は終了です。
C++側の説明は以上になります。
プロジェクトをすべて保存しましょう。
![](https://assets.st-note.com/img/1643032434768-PpAi7sgeis.png?width=1200)
Visual StudioのSolutionもすべて保存しましょう。
![](https://assets.st-note.com/img/1643032442267-L8644qWmHw.png)
まとめ
まだEarry Accessだからか、変な挙動をするのは仕方がない。
EA特有の変な挙動なのか、UE4で同じことをして確認しないといけませんね。
行ったり来たりで少しずつ前に進みましょう。
参照URL
UE4から始めるC++&Blueprint 進捗とロードマップ
Zennで進捗報告を行い、GitHubでロードマップを公開中です。
よかったら覗いてみてください。