C++ クラス ウィザード を使用して、新規 C++ クラスをプロジェクトへ簡単に追加できます。新規クラスを派生させるクラスを選択した後に、 ウィザードが必要なヘッダファイルとソースファイルを設定します。これがプロジェクトに初めて追加したコードの場合、プロジェクトはコード プロジェクトに変換されます。コード プロジェクトは、 ソース コードを含むゲーム モジュールを作成します。これは、アンリアル エディタにゲーム モジュールが存在することを知らせ、Visual Studio または Xcode から C++ の変更を読み込むことができるようにします。マイナーなコード変更はエディタ内部でコンパイルすることができます。
C++ コードのみを使用した LightSwitch クラスは LightSwitchCodeOnly と名付けられます。これについては以下で説明します。
クラス設定
LightSwitchCodeOnly クラスは C++ クラス ウィザードで、 Actor を親クラスとして選択して作成しました。
ヘッダファイル LightSwitchCodeOnly.h
はクラス宣言を格納します。
UCLASS()
class [PROJECTNAME]_API ALightSwitchCodeOnly : public AActor
{
GENERATED_BODY()
};
C++ Class Wizard で作成されたクラス宣言は、自動的に UCLASS()
マクロが前にきます。
UCLASS()
マクロはエンジンにクラスの存在を知らせます。また、エンジン内のクラスの動作を設定するために、キーワード指定子と併用することも出来ます。
クラス宣言は、変数および/または関数宣言を格納します。これらは UPROPERTY()
マクロと UFUNCTION()
マクロがそれぞれ前にきます。
こうしたマクロは UCLASS()
マクロと似たような役割をします。コンポーネントは UPROPERTY()
マクロでも設定されます。
LightSwitchCodeOnly.h
ファイルでは、C++ は以下のように利用されます。
- PointLight コンポーネントと Sphere コンポーネントを宣言します。PointLight コンポーネントと Sphere コンポーネントを
VisibleAnywhere
に設定します。 つまりこれらのプロパティは LightSwitchCodeOnly アクタの [Details (詳細)] タブで見ることができます。
public:
/** point light component */
UPROPERTY(VisibleAnywhere, Category = "Switch Components")
class UPointLightComponent* PointLight1;
/** sphere component */
UPROPERTY(VisibleAnywhere, Category = "Switch Components")
class USphereComponent* Sphere1;
- コンストラクタを宣言し、コンポーネントと変数に対してデフォルト値を設定できるようにします。:
ALightSwitchCodeOnly();
OnOverlapBegin
とOnOverlapEnd
を宣言します。こうした関数は別のアクタが Sphere コンポーネントに入るまたは離れる時に呼ばれます。これらは異なるシグネチャを持つことに注意してください。
/** called when something enters the sphere component (何かが sphere コンポーネントに入ると呼び出されます。)*/
UFUNCTION()
void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
/** called when something leaves the sphere component (何かが sphere コンポーネントを離れると呼び出されます。) */
UFUNCTION()
void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
ToggleLight
は PointLightComponent の可視性を切り替える関数です。
/** Toggles the light component's visibility*/
UFUNCTION()
void ToggleLight();
DesiredIntensity
変数を宣言して、VisibleAnywhere
指定子でどこにでも表示できるようにします。LightSwitchCodeOnly アクタの [Details (詳細)] タブで [Switch Variables] カテゴリに表示されます。 サブオブジェクトではない変数、例えばこの float 値などは、VisibleAnywhere
指定子によって変数が [詳細] タブに表示されるようにします。他にも使用可能なものとして、EditAnywhere
指定子がありますが、DesiredIntensity
変数はアクタがレベルへ追加された時のみ正しく使用されるため、変数は編集可能である必要はありません。
/** the desired intensity for the light (ライトの望ましい明るさ) */
UPROPERTY(VisibleAnywhere, Category="Switch Variables")
float DesiredIntensity;
最終的なヘッダファイルは以下の通りです。
LightSwitchCodeOnly.h
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/Actor.h"
#include "LightSwitchCodeOnly.generated.h"
/**
*
*/
UCLASS()
class [PROJECTNAME]_API ALightSwitchCodeOnly : public AActor
{
GENERATED_BODY()
public:
/** point light component */
UPROPERTY(VisibleAnywhere, Category = "Switch Components")
class UPointLightComponent* PointLight1;
/** sphere component */
UPROPERTY(VisibleAnywhere, Category = "Switch Components")
class USphereComponent* Sphere1;
ALightSwitchCodeOnly();
/** called when something enters the sphere component */
UFUNCTION()
void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
/** called when something leaves the sphere component */
UFUNCTION()
void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
/** Toggles the light component's visibility (light コンポーネントの可視性を切り替えます)*/
UFUNCTION()
void ToggleLight();
/** Toggles the light component's visibility*/
UFUNCTION()
void ToggleLight();
/** the desired intensity for the light */
UPROPERTY(VisibleAnywhere, Category = "Switch Variables")
float DesiredIntensity;
};
[C++ Class Wizard] はクラスのソース ファイルも作成します。この場合は、LightSwitchCodeOnly.cpp
ファイルを作成します。デフォルトで、ソース ファイルには基本的なインクルード ファイルを設定します。
クラス コンストラクタを追加して開始します。
ALightSwitchCodeOnly::ALightSwitchCodeOnly()
{
}
LightSwitchCodeOnly
コンストラクタでは、C++ を以下のように利用します。
DesiredIntensity
変数の値を 3000 に設定します。
DesiredIntensity = 3000.0f;
- PointLight コンポーネントを作成し、その変数を設定し (その明るさを
DesiredIntensity
の値に設定することも含む)、それをルートコンポーネントにします。
PointLight1 = CreateDefaultSubobject<UPointLightComponent>(TEXT("PointLight1"));
PointLight1->Intensity = DesiredIntensity;
PointLight1->bVisible = true;
RootComponent = PointLight1;
- Sphere コンポーネントを作成、変数を設定し、PointLight コンポーネントにアタッチします。
Sphere1 = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere1"));
Sphere1->InitSphereRadius(250.0f);
Sphere1->SetupAttachment(RootComponent);
OnOverlap
関数をアクタが Sphere コンポーネントにオーバーラップした時、または Sphere コンポーネントから離れた時に呼ばれるデリゲートとして指定します。
Sphere1->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnOverlapBegin); // set up a notification for when this component overlaps something
Sphere1->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnOverlapEnd); // set up a notification for when this component overlaps something
ソース ファイルにも、クラスのために宣言する関数を定義することができます。例えば、LightSwitchCodeOnly.cpp
には、ToggleLight
を呼び出すことでPointLight コンポーネントの可視性を切り替える OnOverlapBegin
と OnOverlapEnd
が実装されています。クラス コンストラクタと組み合わせるとソースファイルは以下のようになります。
LightSwitchCodeOnly.cpp
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
#include "BasicClasses.h"
#include "LightSwitchCodeOnly.h"
ALightSwitchCodeOnly::ALightSwitchCodeOnly()
{
DesiredIntensity = 3000.0f;
PointLight1 = CreateDefaultSubobject<UPointLightComponent>(TEXT("PointLight1"));
PointLight1->Intensity = DesiredIntensity;
PointLight1->bVisible = true;
RootComponent = PointLight1;
Sphere1 = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere1"));
Sphere1->InitSphereRadius(250.0f);
Sphere1->SetupAttachment(RootComponent);
Sphere1->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnOverlapBegin); // set up a notification for when this component overlaps something
Sphere1->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnOverlapEnd); // set up a notification for when this component overlaps something
}
void ALightSwitchCodeOnly::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && (OtherActor != this) && OtherComp)
{
ToggleLight();
}
}
void ALightSwitchCodeOnly::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (OtherActor && (OtherActor != this) && OtherComp)
{
ToggleLight();
}
}
void ALightSwitchCodeOnly::ToggleLight()
{
PointLight1->ToggleVisibility();
}
BasicClasses.h
は、そのクラスがセットアップされたプロジェクト名を参照しています。
これが空のプロジェクトに追加した最初のコード クラスである場合は、エディタを閉じて、Visual Studio または Xcode でプロジェクトをコンパイルした後に、エディタを開いてプロジェクトを再び開く必要があります。 ゲームモジュールが適切に作成されて再ロードされていることを確認するためです。プロジェクトを開くために使用しているアンリアル エディタの実行ファイルのバージョンが Build Configuration と同じであることの確認も重要となります。ビルド設定とプロジェクトのコンパイルに関する詳細は、 ゲーム プロジェクトのコンパイル ページをご覧ください。
コードを既存の C++ プロジェクトに追加する場合は、ホットリロード機能を使用してエディタ内で新規コードをコンパイルすることができます。
C++ クラスは、C++ クラスに加えてブループリント クラスで拡張することができます。C++ Class Wizard の Show All Classes チェックボックスのチェックを入れた後、およびブループリント クラスの作成時に、 [Pick Parent Class] ウィンドウの [Custom Classes] セクションの [Show All Classes] にチェックを入れた後に表示されます。
「LightSwitchCodeOnly」クラスは、クラス ビューア に格納されて、ここからレベルへドラッグすることができます。クラスビューアを使用したレベルでのアクタの配置については、アクタの配置 のドキュメントをご覧ください。