Unreal Engine の C++ 開発の基礎知識
Unreal の C++ とは:ブループリントとの違いを徹底解説
Unreal Engine の C++は、エンジンの完全な機能にアクセスできる強力なプログラミング手法です。ブループリントと比較して、以下のような特徴があります:
特徴 | Unreal C++ | ブループリント |
---|---|---|
パフォーマンス | 高速(ネイティブコード) | 比較的低速(インタープリタ実行) |
デバッグ機能 | 完全なデバッグツール | 限定的 |
バージョン管理 | Git等で容易に管理可能 | マージが困難 |
学習曲線 | 急(C++の知識が必要) | 緩やか(視覚的で直感的) |
実行速度 | 最大10倍以上高速 | 基準値 |
C++を選択すべきケース:
- パフォーマンスが重要な機能の実装
- 複雑なアルゴリズムの実装
- 大規模なシステムの設計
- プラグイン開発
開発環境のセットアップ手順
- 必要なツールのインストール:
- Visual Studio 2022(推奨)
- Unreal Engine(Epic Games Launcher経由)
- Visual Studio用Unreal Engine拡張機能
- Visual Studioの設定:
// Visual Studioの推奨設定
// Tools > Options > Text Editor > C/C++ > Advanced
// "Disable External Dependencies Folder" = True
// "Disable Solution Explorer Database" = True
// Visual Studioの推奨設定
// Tools > Options > Text Editor > C/C++ > Advanced
// "Disable External Dependencies Folder" = True
// "Disable Solution Explorer Database" = True
// Visual Studioの推奨設定 // Tools > Options > Text Editor > C/C++ > Advanced // "Disable External Dependencies Folder" = True // "Disable Solution Explorer Database" = True
- プロジェクト固有の設定:
// プロジェクトの.uprojectファイルに追加
{
"Modules": [
{
"Name": "YourProjectName",
"Type": "Runtime",
"LoadingPhase": "Default"
}
]
}
// プロジェクトの.uprojectファイルに追加
{
"Modules": [
{
"Name": "YourProjectName",
"Type": "Runtime",
"LoadingPhase": "Default"
}
]
}
// プロジェクトの.uprojectファイルに追加 { "Modules": [ { "Name": "YourProjectName", "Type": "Runtime", "LoadingPhase": "Default" } ] }
プロジェクト作成からビルドまでの流れ
- プロジェクト作成:
// 1. Epic Games Launcherから新規プロジェクト作成
// 2. C++クラスの追加
class AMyActor : public AActor
{
GENERATED_BODY()
public:
AMyActor();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
};
// 1. Epic Games Launcherから新規プロジェクト作成
// 2. C++クラスの追加
class AMyActor : public AActor
{
GENERATED_BODY()
public:
AMyActor();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
};
// 1. Epic Games Launcherから新規プロジェクト作成 // 2. C++クラスの追加 class AMyActor : public AActor { GENERATED_BODY() public: AMyActor(); protected: virtual void BeginPlay() override; public: virtual void Tick(float DeltaTime) override; };
- ビルドプロセス:
// ビルド手順
// 1. Visual StudioでBuild Solutionを実行
// または
// 2. Unreal Editorで「Compile」ボタンをクリック
// モジュール定義例(Build.cs)
public class YourProject : ModuleRules
{
public YourProject(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
}
}
// ビルド手順
// 1. Visual StudioでBuild Solutionを実行
// または
// 2. Unreal Editorで「Compile」ボタンをクリック
// モジュール定義例(Build.cs)
public class YourProject : ModuleRules
{
public YourProject(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
}
}
// ビルド手順 // 1. Visual StudioでBuild Solutionを実行 // または // 2. Unreal Editorで「Compile」ボタンをクリック // モジュール定義例(Build.cs) public class YourProject : ModuleRules { public YourProject(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" }); } }
- デバッグ準備:
// デバッグログの追加
void AMyActor::BeginPlay()
{
Super::BeginPlay();
// 様々なログレベルの使用例
UE_LOG(LogTemp, Warning, TEXT("Warning Message"));
UE_LOG(LogTemp, Error, TEXT("Error Message"));
// 画面上にデバッグメッセージを表示
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Debug Message"));
}
// デバッグログの追加
void AMyActor::BeginPlay()
{
Super::BeginPlay();
// 様々なログレベルの使用例
UE_LOG(LogTemp, Warning, TEXT("Warning Message"));
UE_LOG(LogTemp, Error, TEXT("Error Message"));
// 画面上にデバッグメッセージを表示
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Debug Message"));
}
// デバッグログの追加 void AMyActor::BeginPlay() { Super::BeginPlay(); // 様々なログレベルの使用例 UE_LOG(LogTemp, Warning, TEXT("Warning Message")); UE_LOG(LogTemp, Error, TEXT("Error Message")); // 画面上にデバッグメッセージを表示 GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Debug Message")); }
重要なポイント:
- モジュール依存関係を適切に管理する
- ホットリロード機能を活用して開発効率を上げる
- プロジェクトの整合性を保つためにビルド設定を理解する
- デバッグツールを効果的に活用する
開発環境の整備とビルドプロセスの理解は、スムーズなUnreal C++開発の基盤となります。次のセクションでは、これらの基礎知識を活かした実践的なプログラミング手法について解説します。
Unreal C++ プログラミングの実践的手法
アクターとコンポーネントの実装方法
アクターとコンポーネントは、Unreal Engineのゲームプレイの基本となる要素です。以下に実践的な実装例を示します:
// カスタムアクターの実装例
UCLASS()
class MYGAME_API ACustomActor : public AActor
{
GENERATED_BODY()
public:
// コンストラクタ
ACustomActor();
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MeshComponent;
UPROPERTY(EditAnywhere, Category = "Movement")
float MovementSpeed = 100.0f;
// カスタムイベント
UFUNCTION(BlueprintCallable, Category = "Actions")
void CustomAction();
protected:
virtual void BeginPlay() override;
virtual void Tick(float DeltaTime) override;
private:
FVector InitialLocation;
};
// 実装
ACustomActor::ACustomActor()
{
PrimaryActorTick.bCanEverTick = true;
// コンポーネントの作成と設定
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
RootComponent = MeshComponent;
}
// カスタムアクターの実装例
UCLASS()
class MYGAME_API ACustomActor : public AActor
{
GENERATED_BODY()
public:
// コンストラクタ
ACustomActor();
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MeshComponent;
UPROPERTY(EditAnywhere, Category = "Movement")
float MovementSpeed = 100.0f;
// カスタムイベント
UFUNCTION(BlueprintCallable, Category = "Actions")
void CustomAction();
protected:
virtual void BeginPlay() override;
virtual void Tick(float DeltaTime) override;
private:
FVector InitialLocation;
};
// 実装
ACustomActor::ACustomActor()
{
PrimaryActorTick.bCanEverTick = true;
// コンポーネントの作成と設定
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
RootComponent = MeshComponent;
}
// カスタムアクターの実装例 UCLASS() class MYGAME_API ACustomActor : public AActor { GENERATED_BODY() public: // コンストラクタ ACustomActor(); UPROPERTY(VisibleAnywhere) UStaticMeshComponent* MeshComponent; UPROPERTY(EditAnywhere, Category = "Movement") float MovementSpeed = 100.0f; // カスタムイベント UFUNCTION(BlueprintCallable, Category = "Actions") void CustomAction(); protected: virtual void BeginPlay() override; virtual void Tick(float DeltaTime) override; private: FVector InitialLocation; }; // 実装 ACustomActor::ACustomActor() { PrimaryActorTick.bCanEverTick = true; // コンポーネントの作成と設定 MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent")); RootComponent = MeshComponent; }
コンポーネントの効果的な使用方法:
// カスタムコンポーネントの実装
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class MYGAME_API UCustomComponent : public UActorComponent
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
float Health = 100.0f;
UFUNCTION(BlueprintCallable, Category = "Damage")
void ApplyDamage(float DamageAmount);
};
// カスタムコンポーネントの実装
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class MYGAME_API UCustomComponent : public UActorComponent
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats")
float Health = 100.0f;
UFUNCTION(BlueprintCallable, Category = "Damage")
void ApplyDamage(float DamageAmount);
};
// カスタムコンポーネントの実装 UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) class MYGAME_API UCustomComponent : public UActorComponent { GENERATED_BODY() public: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stats") float Health = 100.0f; UFUNCTION(BlueprintCallable, Category = "Damage") void ApplyDamage(float DamageAmount); };
ゲームプレイフレームワークの活用術
ゲームプレイフレームワークを効果的に活用するための主要なポイント:
- GameMode の実装:
UCLASS()
class MYGAME_API ACustomGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
// ゲーム状態の管理
UFUNCTION(BlueprintCallable)
void StartNewRound();
UPROPERTY(EditDefaultsOnly, Category = "Game Rules")
int32 MaxPlayers = 4;
protected:
virtual void BeginPlay() override;
// カスタムゲームルールの実装
UFUNCTION()
void HandleMatchStart();
};
UCLASS()
class MYGAME_API ACustomGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
// ゲーム状態の管理
UFUNCTION(BlueprintCallable)
void StartNewRound();
UPROPERTY(EditDefaultsOnly, Category = "Game Rules")
int32 MaxPlayers = 4;
protected:
virtual void BeginPlay() override;
// カスタムゲームルールの実装
UFUNCTION()
void HandleMatchStart();
};
UCLASS() class MYGAME_API ACustomGameMode : public AGameModeBase { GENERATED_BODY() public: // ゲーム状態の管理 UFUNCTION(BlueprintCallable) void StartNewRound(); UPROPERTY(EditDefaultsOnly, Category = "Game Rules") int32 MaxPlayers = 4; protected: virtual void BeginPlay() override; // カスタムゲームルールの実装 UFUNCTION() void HandleMatchStart(); };
- PlayerController の拡張:
UCLASS()
class MYGAME_API ACustomPlayerController : public APlayerController
{
GENERATED_BODY()
protected:
// 入力バインディングの設定
virtual void SetupInputComponent() override;
// カスタム入力処理
void HandleCustomAction();
// レプリケーション設定
UFUNCTION(Server, Reliable)
void ServerCustomAction();
};
UCLASS()
class MYGAME_API ACustomPlayerController : public APlayerController
{
GENERATED_BODY()
protected:
// 入力バインディングの設定
virtual void SetupInputComponent() override;
// カスタム入力処理
void HandleCustomAction();
// レプリケーション設定
UFUNCTION(Server, Reliable)
void ServerCustomAction();
};
UCLASS() class MYGAME_API ACustomPlayerController : public APlayerController { GENERATED_BODY() protected: // 入力バインディングの設定 virtual void SetupInputComponent() override; // カスタム入力処理 void HandleCustomAction(); // レプリケーション設定 UFUNCTION(Server, Reliable) void ServerCustomAction(); };
メモリ管理とパフォーマンス最適化の秘訣
- スマートポインタの活用:
// メモリリーク防止のためのスマートポインタ使用例
TSharedPtr<FCustomData> DataPtr;
TUniquePtr<FCustomResource> ResourcePtr;
// オブジェクトプールの実装
UPROPERTY()
TArray<ACustomActor*> ActorPool;
// メモリ効率の良いコンテナ使用
TArray<FCustomStruct, TInlineAllocator<8>> SmallArray;
// メモリリーク防止のためのスマートポインタ使用例
TSharedPtr<FCustomData> DataPtr;
TUniquePtr<FCustomResource> ResourcePtr;
// オブジェクトプールの実装
UPROPERTY()
TArray<ACustomActor*> ActorPool;
// メモリ効率の良いコンテナ使用
TArray<FCustomStruct, TInlineAllocator<8>> SmallArray;
// メモリリーク防止のためのスマートポインタ使用例 TSharedPtr<FCustomData> DataPtr; TUniquePtr<FCustomResource> ResourcePtr; // オブジェクトプールの実装 UPROPERTY() TArray<ACustomActor*> ActorPool; // メモリ効率の良いコンテナ使用 TArray<FCustomStruct, TInlineAllocator<8>> SmallArray;
- パフォーマンス最適化テクニック:
最適化ポイント | 実装方法 | 効果 |
---|---|---|
メモリアロケーション | プールの使用 | フラグメンテーション防止 |
キャッシュ活用 | データの局所性向上 | アクセス速度向上 |
非同期処理 | AsyncTask の活用 | メインスレッド負荷軽減 |
// 非同期処理の実装例
void ACustomActor::HandleHeavyTask()
{
AsyncTask(ENamedThreads::AnyBackgroundHiPriTask, []()
{
// バックグラウンド処理
// ...
// メインスレッドでの処理
AsyncTask(ENamedThreads::GameThread, []()
{
// UI更新など
});
});
}
// 非同期処理の実装例
void ACustomActor::HandleHeavyTask()
{
AsyncTask(ENamedThreads::AnyBackgroundHiPriTask, []()
{
// バックグラウンド処理
// ...
// メインスレッドでの処理
AsyncTask(ENamedThreads::GameThread, []()
{
// UI更新など
});
});
}
// 非同期処理の実装例 void ACustomActor::HandleHeavyTask() { AsyncTask(ENamedThreads::AnyBackgroundHiPriTask, []() { // バックグラウンド処理 // ... // メインスレッドでの処理 AsyncTask(ENamedThreads::GameThread, []() { // UI更新など }); }); }
重要な最適化のポイント:
- ティック関数の最適化:
void ACustomActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// 必要な時だけティックを有効にする
if (!bNeedsTick)
{
SetActorTickEnabled(false);
return;
}
// 距離に基づくティック頻度の調整
if (GetDistanceToPlayer() > FarDistance)
{
PrimaryActorTick.TickInterval = 0.5f;
}
}
void ACustomActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// 必要な時だけティックを有効にする
if (!bNeedsTick)
{
SetActorTickEnabled(false);
return;
}
// 距離に基づくティック頻度の調整
if (GetDistanceToPlayer() > FarDistance)
{
PrimaryActorTick.TickInterval = 0.5f;
}
}
void ACustomActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); // 必要な時だけティックを有効にする if (!bNeedsTick) { SetActorTickEnabled(false); return; } // 距離に基づくティック頻度の調整 if (GetDistanceToPlayer() > FarDistance) { PrimaryActorTick.TickInterval = 0.5f; } }
この実践的な手法を活用することで、効率的かつパフォーマンスの高いUnreal C++開発が可能となります。次のセクションでは、これらの手法とブループリントを組み合わせた効果的な開発戦略について解説します。
C++とブループリントの効果的な併用戦略
それぞれの特徴を考慮した活用のコツ
C++とブループリントの使い分けの基本原則:
機能 | 推奨する実装方法 | 理由 |
---|---|---|
コアシステム | C++ | パフォーマンスと保守性の確保 |
UI操作 | ブループリント | 迅速な開発と視覚的な調整 |
アニメーション制御 | ブループリント | 直感的な制御とアーティスト対応 |
数値計算処理 | C++ | 処理速度の最適化 |
実装例:
// C++側での基本機能の実装
UCLASS(Blueprintable)
class MYGAME_API AHybridCharacter : public ACharacter
{
GENERATED_BODY()
public:
// ブループリントから呼び出し可能な関数
UFUNCTION(BlueprintCallable, Category = "Character")
float CalculateDamage(float BaseAmount, float Multiplier);
// ブループリントでオーバーライド可能なイベント
UFUNCTION(BlueprintNativeEvent, Category = "Character")
void OnDamageReceived(float Amount);
virtual void OnDamageReceived_Implementation(float Amount);
// ブループリントから監視可能なプロパティ
UPROPERTY(BlueprintReadWrite, Category = "Stats")
float Health;
};
// C++側での基本機能の実装
UCLASS(Blueprintable)
class MYGAME_API AHybridCharacter : public ACharacter
{
GENERATED_BODY()
public:
// ブループリントから呼び出し可能な関数
UFUNCTION(BlueprintCallable, Category = "Character")
float CalculateDamage(float BaseAmount, float Multiplier);
// ブループリントでオーバーライド可能なイベント
UFUNCTION(BlueprintNativeEvent, Category = "Character")
void OnDamageReceived(float Amount);
virtual void OnDamageReceived_Implementation(float Amount);
// ブループリントから監視可能なプロパティ
UPROPERTY(BlueprintReadWrite, Category = "Stats")
float Health;
};
// C++側での基本機能の実装 UCLASS(Blueprintable) class MYGAME_API AHybridCharacter : public ACharacter { GENERATED_BODY() public: // ブループリントから呼び出し可能な関数 UFUNCTION(BlueprintCallable, Category = "Character") float CalculateDamage(float BaseAmount, float Multiplier); // ブループリントでオーバーライド可能なイベント UFUNCTION(BlueprintNativeEvent, Category = "Character") void OnDamageReceived(float Amount); virtual void OnDamageReceived_Implementation(float Amount); // ブループリントから監視可能なプロパティ UPROPERTY(BlueprintReadWrite, Category = "Stats") float Health; };
C++からブループリントを操作する方法
- ブループリントインスタンスの取得と操作:
// ブループリントクラスの参照
UPROPERTY(EditDefaultsOnly, Category = "Blueprint References")
TSubclassOf<UUserWidget> WidgetClass;
// ブループリントウィジェットの生成と表示
void AGameController::ShowWidget()
{
if (WidgetClass)
{
UUserWidget* Widget = CreateWidget<UUserWidget>(GetWorld(), WidgetClass);
Widget->AddToViewport();
// ブループリント関数の呼び出し
if (UFunction* Func = Widget->FindFunction(TEXT("OnWidgetShown")))
{
Widget->ProcessEvent(Func, nullptr);
}
}
}
// ブループリントクラスの参照
UPROPERTY(EditDefaultsOnly, Category = "Blueprint References")
TSubclassOf<UUserWidget> WidgetClass;
// ブループリントウィジェットの生成と表示
void AGameController::ShowWidget()
{
if (WidgetClass)
{
UUserWidget* Widget = CreateWidget<UUserWidget>(GetWorld(), WidgetClass);
Widget->AddToViewport();
// ブループリント関数の呼び出し
if (UFunction* Func = Widget->FindFunction(TEXT("OnWidgetShown")))
{
Widget->ProcessEvent(Func, nullptr);
}
}
}
// ブループリントクラスの参照 UPROPERTY(EditDefaultsOnly, Category = "Blueprint References") TSubclassOf<UUserWidget> WidgetClass; // ブループリントウィジェットの生成と表示 void AGameController::ShowWidget() { if (WidgetClass) { UUserWidget* Widget = CreateWidget<UUserWidget>(GetWorld(), WidgetClass); Widget->AddToViewport(); // ブループリント関数の呼び出し if (UFunction* Func = Widget->FindFunction(TEXT("OnWidgetShown"))) { Widget->ProcessEvent(Func, nullptr); } } }
- イベントディスパッチャーの活用:
// C++側でのイベント定義
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnHealthChanged, float, NewHealth);
UCLASS()
class MYGAME_API AHealthComponent : public UActorComponent
{
GENERATED_BODY()
public:
// ブループリントでバインド可能なイベント
UPROPERTY(BlueprintAssignable, Category = "Events")
FOnHealthChanged OnHealthChanged;
// イベント発火
void UpdateHealth(float NewValue)
{
Health = NewValue;
OnHealthChanged.Broadcast(Health);
}
};
// C++側でのイベント定義
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnHealthChanged, float, NewHealth);
UCLASS()
class MYGAME_API AHealthComponent : public UActorComponent
{
GENERATED_BODY()
public:
// ブループリントでバインド可能なイベント
UPROPERTY(BlueprintAssignable, Category = "Events")
FOnHealthChanged OnHealthChanged;
// イベント発火
void UpdateHealth(float NewValue)
{
Health = NewValue;
OnHealthChanged.Broadcast(Health);
}
};
// C++側でのイベント定義 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnHealthChanged, float, NewHealth); UCLASS() class MYGAME_API AHealthComponent : public UActorComponent { GENERATED_BODY() public: // ブループリントでバインド可能なイベント UPROPERTY(BlueprintAssignable, Category = "Events") FOnHealthChanged OnHealthChanged; // イベント発火 void UpdateHealth(float NewValue) { Health = NewValue; OnHealthChanged.Broadcast(Health); } };
ハイブリッド開発のベストプラクティス
- インターフェースの活用:
// C++インターフェース定義
UINTERFACE(MinimalAPI, Blueprintable)
class UInteractableInterface : public UInterface
{
GENERATED_BODY()
};
class IInteractableInterface
{
GENERATED_BODY()
public:
// ブループリントで実装可能な純粋仮想関数
UFUNCTION(BlueprintNativeEvent, Category = "Interaction")
void OnInteract(AActor* Interactor);
};
// C++インターフェース定義
UINTERFACE(MinimalAPI, Blueprintable)
class UInteractableInterface : public UInterface
{
GENERATED_BODY()
};
class IInteractableInterface
{
GENERATED_BODY()
public:
// ブループリントで実装可能な純粋仮想関数
UFUNCTION(BlueprintNativeEvent, Category = "Interaction")
void OnInteract(AActor* Interactor);
};
// C++インターフェース定義 UINTERFACE(MinimalAPI, Blueprintable) class UInteractableInterface : public UInterface { GENERATED_BODY() }; class IInteractableInterface { GENERATED_BODY() public: // ブループリントで実装可能な純粋仮想関数 UFUNCTION(BlueprintNativeEvent, Category = "Interaction") void OnInteract(AActor* Interactor); };
- パフォーマンス最適化のガイドライン:
- ブループリントでの重い処理を特定
// パフォーマンスモニタリング機能の実装
UCLASS()
class MYGAME_API UPerformanceMonitor : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Performance")
static void StartMonitoring(const FString& SectionName)
{
SCOPE_CYCLE_COUNTER(STAT_BlueprintPerformance);
// モニタリング処理
}
};
// パフォーマンスモニタリング機能の実装
UCLASS()
class MYGAME_API UPerformanceMonitor : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Performance")
static void StartMonitoring(const FString& SectionName)
{
SCOPE_CYCLE_COUNTER(STAT_BlueprintPerformance);
// モニタリング処理
}
};
// パフォーマンスモニタリング機能の実装 UCLASS() class MYGAME_API UPerformanceMonitor : public UObject { GENERATED_BODY() public: UFUNCTION(BlueprintCallable, Category = "Performance") static void StartMonitoring(const FString& SectionName) { SCOPE_CYCLE_COUNTER(STAT_BlueprintPerformance); // モニタリング処理 } };
- 保守性を考慮した設計パターン:
// ファクトリーパターンの実装例
UCLASS()
class MYGAME_API AActorFactory : public AActor
{
GENERATED_BODY()
public:
// C++で基本ロジックを実装
UFUNCTION(BlueprintCallable, Category = "Factory")
AActor* CreateActor(TSubclassOf<AActor> ActorClass);
// ブループリントでカスタマイズ可能な処理
UFUNCTION(BlueprintImplementableEvent, Category = "Factory")
void OnActorCreated(AActor* NewActor);
};
// ファクトリーパターンの実装例
UCLASS()
class MYGAME_API AActorFactory : public AActor
{
GENERATED_BODY()
public:
// C++で基本ロジックを実装
UFUNCTION(BlueprintCallable, Category = "Factory")
AActor* CreateActor(TSubclassOf<AActor> ActorClass);
// ブループリントでカスタマイズ可能な処理
UFUNCTION(BlueprintImplementableEvent, Category = "Factory")
void OnActorCreated(AActor* NewActor);
};
// ファクトリーパターンの実装例 UCLASS() class MYGAME_API AActorFactory : public AActor { GENERATED_BODY() public: // C++で基本ロジックを実装 UFUNCTION(BlueprintCallable, Category = "Factory") AActor* CreateActor(TSubclassOf<AActor> ActorClass); // ブループリントでカスタマイズ可能な処理 UFUNCTION(BlueprintImplementableEvent, Category = "Factory") void OnActorCreated(AActor* NewActor); };
開発プロジェクトでの実践的なポイント:
- チーム構成に応じた役割分担
- プログラマー: コアシステムのC++実装
- デザイナー: ゲームプレイロジックのブループリント実装
- バージョン管理の効率化
- C++コードとブループリントの変更を分離
- マージコンフリクトの最小化
- デバッグ戦略
- C++側でのログ出力機能の実装
- ブループリント側での視覚的デバッグ
この併用戦略を活用することで、開発効率とパフォーマンスの両立が可能となります。次のセクションでは、より具体的な開発テクニックについて解説します。
実践的なUnreal C++開発テクニック
デバッグとトラブルシューティングの実践手法
- 高度なデバッグ機能の実装:
// カスタムログカテゴリの定義
DECLARE_LOG_CATEGORY_EXTERN(LogCustomGame, Log, All);
DEFINE_LOG_CATEGORY(LogCustomGame);
// デバッグ機能を持つベースクラス
UCLASS()
class MYGAME_API ADebuggableActor : public AActor
{
GENERATED_BODY()
public:
// 条件付きデバッグ表示
void DEBUG_ShowInfo()
{
#if WITH_EDITOR || UE_BUILD_DEBUG
const FString DebugMsg = FString::Printf(TEXT("Actor: %s, State: %s"),
*GetName(), *CurrentState.ToString());
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, DebugMsg);
UE_LOG(LogCustomGame, Warning, TEXT("%s"), *DebugMsg);
#endif
}
// メモリ使用量の追跡
void TrackMemoryUsage()
{
#if UE_BUILD_DEBUG
FPlatformMemory::DumpStats();
#endif
}
};
// カスタムログカテゴリの定義
DECLARE_LOG_CATEGORY_EXTERN(LogCustomGame, Log, All);
DEFINE_LOG_CATEGORY(LogCustomGame);
// デバッグ機能を持つベースクラス
UCLASS()
class MYGAME_API ADebuggableActor : public AActor
{
GENERATED_BODY()
public:
// 条件付きデバッグ表示
void DEBUG_ShowInfo()
{
#if WITH_EDITOR || UE_BUILD_DEBUG
const FString DebugMsg = FString::Printf(TEXT("Actor: %s, State: %s"),
*GetName(), *CurrentState.ToString());
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, DebugMsg);
UE_LOG(LogCustomGame, Warning, TEXT("%s"), *DebugMsg);
#endif
}
// メモリ使用量の追跡
void TrackMemoryUsage()
{
#if UE_BUILD_DEBUG
FPlatformMemory::DumpStats();
#endif
}
};
// カスタムログカテゴリの定義 DECLARE_LOG_CATEGORY_EXTERN(LogCustomGame, Log, All); DEFINE_LOG_CATEGORY(LogCustomGame); // デバッグ機能を持つベースクラス UCLASS() class MYGAME_API ADebuggableActor : public AActor { GENERATED_BODY() public: // 条件付きデバッグ表示 void DEBUG_ShowInfo() { #if WITH_EDITOR || UE_BUILD_DEBUG const FString DebugMsg = FString::Printf(TEXT("Actor: %s, State: %s"), *GetName(), *CurrentState.ToString()); GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, DebugMsg); UE_LOG(LogCustomGame, Warning, TEXT("%s"), *DebugMsg); #endif } // メモリ使用量の追跡 void TrackMemoryUsage() { #if UE_BUILD_DEBUG FPlatformMemory::DumpStats(); #endif } };
- クラッシュ解析ツール:
// クラッシュレポート機能
void UCrashAnalyzer::HandleCrash()
{
// スタックトレースの取得
const SIZE_T StackTraceSize = 65535;
ANSICHAR* StackTrace = (ANSICHAR*)FMemory::Malloc(StackTraceSize);
FPlatformStackWalk::StackWalkAndDump(StackTrace, StackTraceSize, 0);
// クラッシュ情報の記録
UE_LOG(LogCustomGame, Error, TEXT("Crash detected:\n%s"), ANSI_TO_TCHAR(StackTrace));
FMemory::Free(StackTrace);
}
// クラッシュレポート機能
void UCrashAnalyzer::HandleCrash()
{
// スタックトレースの取得
const SIZE_T StackTraceSize = 65535;
ANSICHAR* StackTrace = (ANSICHAR*)FMemory::Malloc(StackTraceSize);
FPlatformStackWalk::StackWalkAndDump(StackTrace, StackTraceSize, 0);
// クラッシュ情報の記録
UE_LOG(LogCustomGame, Error, TEXT("Crash detected:\n%s"), ANSI_TO_TCHAR(StackTrace));
FMemory::Free(StackTrace);
}
// クラッシュレポート機能 void UCrashAnalyzer::HandleCrash() { // スタックトレースの取得 const SIZE_T StackTraceSize = 65535; ANSICHAR* StackTrace = (ANSICHAR*)FMemory::Malloc(StackTraceSize); FPlatformStackWalk::StackWalkAndDump(StackTrace, StackTraceSize, 0); // クラッシュ情報の記録 UE_LOG(LogCustomGame, Error, TEXT("Crash detected:\n%s"), ANSI_TO_TCHAR(StackTrace)); FMemory::Free(StackTrace); }
マルチプレイヤー機能の実装方法
- レプリケーションの基本設定:
UCLASS()
class MYGAME_API ANetworkedActor : public AActor
{
GENERATED_BODY()
public:
ANetworkedActor()
{
bReplicates = true;
bAlwaysRelevant = true;
}
// レプリケート変数
UPROPERTY(Replicated)
float ReplicatedValue;
// レプリケーション条件の設定
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ANetworkedActor, ReplicatedValue);
DOREPLIFETIME_CONDITION(ANetworkedActor, ConditionalValue, COND_OwnerOnly);
}
};
UCLASS()
class MYGAME_API ANetworkedActor : public AActor
{
GENERATED_BODY()
public:
ANetworkedActor()
{
bReplicates = true;
bAlwaysRelevant = true;
}
// レプリケート変数
UPROPERTY(Replicated)
float ReplicatedValue;
// レプリケーション条件の設定
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ANetworkedActor, ReplicatedValue);
DOREPLIFETIME_CONDITION(ANetworkedActor, ConditionalValue, COND_OwnerOnly);
}
};
UCLASS() class MYGAME_API ANetworkedActor : public AActor { GENERATED_BODY() public: ANetworkedActor() { bReplicates = true; bAlwaysRelevant = true; } // レプリケート変数 UPROPERTY(Replicated) float ReplicatedValue; // レプリケーション条件の設定 virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override { Super::GetLifetimeReplicatedProps(OutLifetimeProps); DOREPLIFETIME(ANetworkedActor, ReplicatedValue); DOREPLIFETIME_CONDITION(ANetworkedActor, ConditionalValue, COND_OwnerOnly); } };
- RPCの実装:
UCLASS()
class MYGAME_API ANetworkCharacter : public ACharacter
{
GENERATED_BODY()
public:
// サーバーRPC
UFUNCTION(Server, Reliable)
void ServerPerformAction();
// マルチキャストRPC
UFUNCTION(NetMulticast, Reliable)
void MulticastNotifyAction();
// クライアントRPC
UFUNCTION(Client, Reliable)
void ClientReceiveUpdate(const FGameState& NewState);
protected:
void ServerPerformAction_Implementation()
{
// サーバー側の処理
if (HasAuthority())
{
// アクション実行
MulticastNotifyAction();
}
}
};
UCLASS()
class MYGAME_API ANetworkCharacter : public ACharacter
{
GENERATED_BODY()
public:
// サーバーRPC
UFUNCTION(Server, Reliable)
void ServerPerformAction();
// マルチキャストRPC
UFUNCTION(NetMulticast, Reliable)
void MulticastNotifyAction();
// クライアントRPC
UFUNCTION(Client, Reliable)
void ClientReceiveUpdate(const FGameState& NewState);
protected:
void ServerPerformAction_Implementation()
{
// サーバー側の処理
if (HasAuthority())
{
// アクション実行
MulticastNotifyAction();
}
}
};
UCLASS() class MYGAME_API ANetworkCharacter : public ACharacter { GENERATED_BODY() public: // サーバーRPC UFUNCTION(Server, Reliable) void ServerPerformAction(); // マルチキャストRPC UFUNCTION(NetMulticast, Reliable) void MulticastNotifyAction(); // クライアントRPC UFUNCTION(Client, Reliable) void ClientReceiveUpdate(const FGameState& NewState); protected: void ServerPerformAction_Implementation() { // サーバー側の処理 if (HasAuthority()) { // アクション実行 MulticastNotifyAction(); } } };
パフォーマンス最適化の具体的な手法
- メモリ最適化:
// カスタムアロケータの実装
class FCustomAllocator : public FMalloc
{
public:
virtual void* Malloc(SIZE_T Size, uint32 Alignment) override
{
// メモリプールからの割り当て
return MemoryPool.Allocate(Size, Alignment);
}
virtual void Free(void* Ptr) override
{
// メモリプールへの返却
MemoryPool.Deallocate(Ptr);
}
private:
FMemoryPool MemoryPool;
};
// カスタムアロケータの実装
class FCustomAllocator : public FMalloc
{
public:
virtual void* Malloc(SIZE_T Size, uint32 Alignment) override
{
// メモリプールからの割り当て
return MemoryPool.Allocate(Size, Alignment);
}
virtual void Free(void* Ptr) override
{
// メモリプールへの返却
MemoryPool.Deallocate(Ptr);
}
private:
FMemoryPool MemoryPool;
};
// カスタムアロケータの実装 class FCustomAllocator : public FMalloc { public: virtual void* Malloc(SIZE_T Size, uint32 Alignment) override { // メモリプールからの割り当て return MemoryPool.Allocate(Size, Alignment); } virtual void Free(void* Ptr) override { // メモリプールへの返却 MemoryPool.Deallocate(Ptr); } private: FMemoryPool MemoryPool; };
- プロファイリングツールの活用:
// パフォーマンス計測用マクロ
#define SCOPE_PERFORMANCE_LOG(ScopeName) \
FScopedDurationTimer ScopedTimer([](float Duration) { \
UE_LOG(LogCustomGame, Log, TEXT("%s took %f seconds"), TEXT(ScopeName), Duration); \
});
// 使用例
void APerformanceCriticalActor::HeavyOperation()
{
SCOPE_PERFORMANCE_LOG("HeavyOperation");
// 処理内容
}
// パフォーマンス計測用マクロ
#define SCOPE_PERFORMANCE_LOG(ScopeName) \
FScopedDurationTimer ScopedTimer([](float Duration) { \
UE_LOG(LogCustomGame, Log, TEXT("%s took %f seconds"), TEXT(ScopeName), Duration); \
});
// 使用例
void APerformanceCriticalActor::HeavyOperation()
{
SCOPE_PERFORMANCE_LOG("HeavyOperation");
// 処理内容
}
// パフォーマンス計測用マクロ #define SCOPE_PERFORMANCE_LOG(ScopeName) \ FScopedDurationTimer ScopedTimer([](float Duration) { \ UE_LOG(LogCustomGame, Log, TEXT("%s took %f seconds"), TEXT(ScopeName), Duration); \ }); // 使用例 void APerformanceCriticalActor::HeavyOperation() { SCOPE_PERFORMANCE_LOG("HeavyOperation"); // 処理内容 }
- スレッド処理の最適化:
// 非同期処理の実装
void UAsyncProcessor::ProcessLargeData()
{
AsyncTask(ENamedThreads::AnyBackgroundHiPriTask, [this]()
{
// バックグラウンド処理
FPlatformProcess::Sleep(0.001f); // スレッド占有防止
// メインスレッドでの更新
AsyncTask(ENamedThreads::GameThread, [this]()
{
OnProcessingComplete.Broadcast();
});
});
}
// 非同期処理の実装
void UAsyncProcessor::ProcessLargeData()
{
AsyncTask(ENamedThreads::AnyBackgroundHiPriTask, [this]()
{
// バックグラウンド処理
FPlatformProcess::Sleep(0.001f); // スレッド占有防止
// メインスレッドでの更新
AsyncTask(ENamedThreads::GameThread, [this]()
{
OnProcessingComplete.Broadcast();
});
});
}
// 非同期処理の実装 void UAsyncProcessor::ProcessLargeData() { AsyncTask(ENamedThreads::AnyBackgroundHiPriTask, [this]() { // バックグラウンド処理 FPlatformProcess::Sleep(0.001f); // スレッド占有防止 // メインスレッドでの更新 AsyncTask(ENamedThreads::GameThread, [this]() { OnProcessingComplete.Broadcast(); }); }); }
最適化のチェックリスト:
- メモリ使用量の削減
- オブジェクトプーリング
- メモリフラグメンテーション対策
- アセット読み込みの最適化
- CPU負荷の軽減
- ティック頻度の調整
- 条件付き処理の実装
- キャッシュの活用
- ネットワーク最適化
- レプリケーション頻度の調整
- データ圧縮
- 優先度付けされた更新
これらの開発テクニックを適切に組み合わせることで、高品質で安定したゲーム開発が可能となります。次のセクションでは、実務での具体的な活用方法について解説します。
現場で活きるUnreal C++開発のためのヒント
プロジェクト規模での設計パターンの活用
- サービスロケーターパターン:
// ゲームサービスの管理
UCLASS()
class MYGAME_API UGameServiceLocator : public UGameInstanceSubsystem
{
GENERATED_BODY()
public:
// サービスの登録
template<typename T>
void RegisterService(TScriptInterface<T> Service)
{
Services.Add(T::StaticClass(), Service);
}
// サービスの取得
template<typename T>
TScriptInterface<T> GetService()
{
return Services.FindRef(T::StaticClass());
}
private:
TMap<UClass*, TScriptInterface<IInterface>> Services;
};
// ゲームサービスの管理
UCLASS()
class MYGAME_API UGameServiceLocator : public UGameInstanceSubsystem
{
GENERATED_BODY()
public:
// サービスの登録
template<typename T>
void RegisterService(TScriptInterface<T> Service)
{
Services.Add(T::StaticClass(), Service);
}
// サービスの取得
template<typename T>
TScriptInterface<T> GetService()
{
return Services.FindRef(T::StaticClass());
}
private:
TMap<UClass*, TScriptInterface<IInterface>> Services;
};
// ゲームサービスの管理 UCLASS() class MYGAME_API UGameServiceLocator : public UGameInstanceSubsystem { GENERATED_BODY() public: // サービスの登録 template<typename T> void RegisterService(TScriptInterface<T> Service) { Services.Add(T::StaticClass(), Service); } // サービスの取得 template<typename T> TScriptInterface<T> GetService() { return Services.FindRef(T::StaticClass()); } private: TMap<UClass*, TScriptInterface<IInterface>> Services; };
- コマンドパターン:
// コマンドインターフェース
UINTERFACE()
class UGameCommand : public UInterface
{
GENERATED_BODY()
};
class IGameCommand
{
GENERATED_BODY()
public:
virtual void Execute() = 0;
virtual void Undo() = 0;
};
// コマンドマネージャー
UCLASS()
class MYGAME_API UCommandManager : public UObject
{
GENERATED_BODY()
public:
void ExecuteCommand(TScriptInterface<IGameCommand> Command)
{
Command->Execute();
CommandHistory.Add(Command);
}
void UndoLastCommand()
{
if (CommandHistory.Num() > 0)
{
auto LastCommand = CommandHistory.Pop();
LastCommand->Undo();
}
}
private:
TArray<TScriptInterface<IGameCommand>> CommandHistory;
};
// コマンドインターフェース
UINTERFACE()
class UGameCommand : public UInterface
{
GENERATED_BODY()
};
class IGameCommand
{
GENERATED_BODY()
public:
virtual void Execute() = 0;
virtual void Undo() = 0;
};
// コマンドマネージャー
UCLASS()
class MYGAME_API UCommandManager : public UObject
{
GENERATED_BODY()
public:
void ExecuteCommand(TScriptInterface<IGameCommand> Command)
{
Command->Execute();
CommandHistory.Add(Command);
}
void UndoLastCommand()
{
if (CommandHistory.Num() > 0)
{
auto LastCommand = CommandHistory.Pop();
LastCommand->Undo();
}
}
private:
TArray<TScriptInterface<IGameCommand>> CommandHistory;
};
// コマンドインターフェース UINTERFACE() class UGameCommand : public UInterface { GENERATED_BODY() }; class IGameCommand { GENERATED_BODY() public: virtual void Execute() = 0; virtual void Undo() = 0; }; // コマンドマネージャー UCLASS() class MYGAME_API UCommandManager : public UObject { GENERATED_BODY() public: void ExecuteCommand(TScriptInterface<IGameCommand> Command) { Command->Execute(); CommandHistory.Add(Command); } void UndoLastCommand() { if (CommandHistory.Num() > 0) { auto LastCommand = CommandHistory.Pop(); LastCommand->Undo(); } } private: TArray<TScriptInterface<IGameCommand>> CommandHistory; };
コード品質を維持するためのプラクティス
- 単体テストの実装:
// テストケースの例
IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMathUtilsTest, "MathUtils.BasicOperations", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter)
bool FMathUtilsTest::RunTest(const FString& Parameters)
{
// テストケース
UMathUtils* MathUtils = NewObject<UMathUtils>();
TestEqual("Addition Test", MathUtils->Add(2, 3), 5);
TestEqual("Multiplication Test", MathUtils->Multiply(4, 3), 12);
return true;
}
// テストケースの例
IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMathUtilsTest, "MathUtils.BasicOperations", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter)
bool FMathUtilsTest::RunTest(const FString& Parameters)
{
// テストケース
UMathUtils* MathUtils = NewObject<UMathUtils>();
TestEqual("Addition Test", MathUtils->Add(2, 3), 5);
TestEqual("Multiplication Test", MathUtils->Multiply(4, 3), 12);
return true;
}
// テストケースの例 IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMathUtilsTest, "MathUtils.BasicOperations", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) bool FMathUtilsTest::RunTest(const FString& Parameters) { // テストケース UMathUtils* MathUtils = NewObject<UMathUtils>(); TestEqual("Addition Test", MathUtils->Add(2, 3), 5); TestEqual("Multiplication Test", MathUtils->Multiply(4, 3), 12); return true; }
- コードレビューチェックリスト:
項目 | チェックポイント | 重要度 |
---|---|---|
命名規則 | Unrealの命名規則に準拠 | 高 |
SOLID原則 | 単一責任の原則を遵守 | 高 |
パフォーマンス | 重い処理の最適化 | 中 |
メモリ管理 | リソースリークの防止 | 高 |
ドキュメント | コメントの適切な記述 | 中 |
- 静的解析の活用:
// 静的解析のための属性
UCLASS(meta=(DeprecatedFunction))
class MYGAME_API ULegacyClass : public UObject
{
GENERATED_BODY()
public:
// 非推奨関数の明示
UFUNCTION(meta=(DeprecatedFunction, DeprecationMessage="Use NewFunction instead"))
void OldFunction();
};
// 静的解析のための属性
UCLASS(meta=(DeprecatedFunction))
class MYGAME_API ULegacyClass : public UObject
{
GENERATED_BODY()
public:
// 非推奨関数の明示
UFUNCTION(meta=(DeprecatedFunction, DeprecationMessage="Use NewFunction instead"))
void OldFunction();
};
// 静的解析のための属性 UCLASS(meta=(DeprecatedFunction)) class MYGAME_API ULegacyClass : public UObject { GENERATED_BODY() public: // 非推奨関数の明示 UFUNCTION(meta=(DeprecatedFunction, DeprecationMessage="Use NewFunction instead")) void OldFunction(); };
実務で使える便利なプラグインとツール
- エディタ拡張の実装:
// カスタムエディタツール
UCLASS()
class MYGAME_API UCustomEditorTool : public UEditorUtilityWidget
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Editor Tools")
void BatchProcessAssets()
{
// アセット一括処理
FAssetRegistryModule& AssetRegistryModule =
FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
// アセットの検索と処理
TArray<FAssetData> AssetList;
AssetRegistryModule.Get().GetAssetsByClass(
UStaticMesh::StaticClass()->GetFName(), AssetList);
for (const FAssetData& Asset : AssetList)
{
ProcessAsset(Asset);
}
}
};
// カスタムエディタツール
UCLASS()
class MYGAME_API UCustomEditorTool : public UEditorUtilityWidget
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Editor Tools")
void BatchProcessAssets()
{
// アセット一括処理
FAssetRegistryModule& AssetRegistryModule =
FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
// アセットの検索と処理
TArray<FAssetData> AssetList;
AssetRegistryModule.Get().GetAssetsByClass(
UStaticMesh::StaticClass()->GetFName(), AssetList);
for (const FAssetData& Asset : AssetList)
{
ProcessAsset(Asset);
}
}
};
// カスタムエディタツール UCLASS() class MYGAME_API UCustomEditorTool : public UEditorUtilityWidget { GENERATED_BODY() public: UFUNCTION(BlueprintCallable, Category = "Editor Tools") void BatchProcessAssets() { // アセット一括処理 FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry"); // アセットの検索と処理 TArray<FAssetData> AssetList; AssetRegistryModule.Get().GetAssetsByClass( UStaticMesh::StaticClass()->GetFName(), AssetList); for (const FAssetData& Asset : AssetList) { ProcessAsset(Asset); } } };
- デバッグ支援ツール:
// ゲーム内デバッグコンソール
UCLASS()
class MYGAME_API UDebugConsole : public UObject
{
GENERATED_BODY()
public:
// コマンド登録
void RegisterCommand(const FString& Command, const FDebugCommandDelegate& Delegate)
{
Commands.Add(Command, Delegate);
}
// コマンド実行
void ExecuteCommand(const FString& CommandLine)
{
FString Command;
FString Parameters;
CommandLine.Split(TEXT(" "), &Command, &Parameters);
if (auto Delegate = Commands.Find(Command))
{
Delegate->Execute(Parameters);
}
}
private:
TMap<FString, FDebugCommandDelegate> Commands;
};
// ゲーム内デバッグコンソール
UCLASS()
class MYGAME_API UDebugConsole : public UObject
{
GENERATED_BODY()
public:
// コマンド登録
void RegisterCommand(const FString& Command, const FDebugCommandDelegate& Delegate)
{
Commands.Add(Command, Delegate);
}
// コマンド実行
void ExecuteCommand(const FString& CommandLine)
{
FString Command;
FString Parameters;
CommandLine.Split(TEXT(" "), &Command, &Parameters);
if (auto Delegate = Commands.Find(Command))
{
Delegate->Execute(Parameters);
}
}
private:
TMap<FString, FDebugCommandDelegate> Commands;
};
// ゲーム内デバッグコンソール UCLASS() class MYGAME_API UDebugConsole : public UObject { GENERATED_BODY() public: // コマンド登録 void RegisterCommand(const FString& Command, const FDebugCommandDelegate& Delegate) { Commands.Add(Command, Delegate); } // コマンド実行 void ExecuteCommand(const FString& CommandLine) { FString Command; FString Parameters; CommandLine.Split(TEXT(" "), &Command, &Parameters); if (auto Delegate = Commands.Find(Command)) { Delegate->Execute(Parameters); } } private: TMap<FString, FDebugCommandDelegate> Commands; };
実務での効率化のポイント:
- コードテンプレートの活用
- 頻出パターンのテンプレート化
- カスタムクラスウィザードの作成
- ビルドパイプラインの最適化
- ビルドスクリプトの自動化
- 依存関係の最適化
- チーム開発の効率化
- コーディング規約の自動チェック
- レビュープロセスの標準化
これらの実務的なヒントを活用することで、プロジェクトの品質と開発効率を大きく向上させることができます。