どのようなブループリントでも C++ プロジェクトに変換できます。C++ を追加したいブループリント プロジェクトがあれば、次のセクションに従って新規 C++ クラスを作成すると、エディタによりそのコードのための環境が自動的に構成されます。
また、C++ プロジェクトを使用しても、引き続きブループリントが使用できます。C++ プロジェクトではブループリントの代わりに C++ でプロジェクトの基本クラスを構成するだけです。
2. 新規 C++ クラスを作成する
Unreal Editor で、[File (ファイル)] ドロップダウン メニューをクリックして、[New C++ Class... (新規 C++ クラス...)] コマンドを選択します。
クリックしてフルサイズ表示。
[Choose Parent Class (親クラスを選択)] メニューが表示されます既存のクラスを拡張すると、そのクラスの機能を自分のクラスに追加することができます。ワールドに配置できる最も基本的な種類のオブジェクトであるため アクタ を選択して、[Next (次へ)] をクリックします。
クリックしてフルサイズ表示。
[Name Your New Actor (新しいアクタに名前を付ける)] メニューで、アクタに「 FloatingActor 」と名前を付けて、[Create Class (クラスの作成)] をクリックします。
これで C++ のみを使用して、最初の Actor クラスを作成できました。とてもシンプルなオブジェクトであり、C++ のソース コードを使う機能のいくつかでしたが、
ゲームの C++ コードの作成、編集、コンパイルに関する基本事項をすべて確認することができました。今度は、より複雑なゲームプレイのプログラミングに挑戦する準備が整ったので、以下のことに取り組むことをお勧めします。
6. 応用編
シンプルな C++ アクタのビルド方法を理解できたので、設定可能な項目をもっと増やしてみましょう。たとえば、動作を制御するための変数を追加できます。
...
NewLocation.Z += DeltaHeight * FloatSpeed; //Scale our height by FloatSpeed
float DeltaRotation = DeltaTime * RotationSpeed; //Rotate by a number of degrees equal to RotationSpeed each second
...
// Copyright 1998-2019 Epic Games, Inc.All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FloatingActor.generated.h"
UCLASS()
class QUICKSTART_API AFloatingActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFloatingActor();
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* VisualMesh;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
FloatingActor.cpp
// Copyright 1998-2019 Epic Games, Inc.All Rights Reserved.
#include "FloatingActor.h"
// Sets default values
AFloatingActor::AFloatingActor()
{
// 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;
VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
if (CubeVisualAsset.Succeeded())
{
VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
}
}
// Called when the game starts or when spawned
void AFloatingActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFloatingActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector NewLocation = GetActorLocation();
FRotator NewRotation = GetActorRotation();
float RunningTime = GetGameTimeSinceCreation();
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
NewLocation.Z += DeltaHeight * 20.0f; //Scale our height by a factor of 20
float DeltaRotation = DeltaTime * 20.0f; //Rotate by 20 degrees per second
NewRotation.Yaw += DeltaRotation;
SetActorLocationAndRotation(NewLocation, NewRotation);
}