Language:
Page Info
Skill Level:
Engine Version:

3 - Write and Compile C++ Code

  1. In Visual Studio, we'll use the Solution Explorer pane to find our newly-created C++ files. In our example, they will be named FloatingActor.cpp and FloatingActor.h and will be inside the QuickStart project.

    SolutionExplorer_Files.png

    We're about to write code. All code used in this tutorial will be found at bottom of the page in its current state as of completing that page's instructions.

  2. In FloatingActor.h, we'll add the following code just before the closing brace and semicolon at the end of the file:

    float RunningTime;
  3. Switching to FloatingActor.cpp, we'll add the following code just before the closing brace at the bottom of AFloatingActor::Tick:

    FVector NewLocation = GetActorLocation();
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20
    RunningTime += DeltaTime;
    SetActorLocation(NewLocation);

    The code we've just written will cause FloatingActors to bob up and down smoothly, using the RunningTime variable we created to keep track of our movement over time.

Choose Your Operating System

Windows

Mac OS

  • Now that we're done coding, we can compile by right-clicking our project in the Solution Browser and selecting the Build command, or by pressing the Compile button in the Unreal Editor. Once the compile succeeds, Unreal Editor will automatically load our changes.

    CompileFromVS.png

    Compiling from Visual Studio.

    CompileFromEditor.png

    Compiling from the Unreal Editor.

When building in Xcode, you are only compiling the game project, not the Editor.

  • Now that we're done coding, we can compile our project using XCode's Debug Configuration by clicking the Product > Build option.

    ProjectBuild_mac.png

    Compiling using XCode's Debug Configuration.

  • We can also compile using XCode's Development Configuration by clicking the Product > Build For > Profiling option.

    ProjectBuildProfile_mac.png

    Compiling using XCode's Development Configuration.

    CompileFromEditor.png

    Compiling from the Unreal Editor.

When running the binary Editor, it is important to add the -game flag if you rebuilt your project in any Uncooked configuration, and the -debug flag if you rebuilt your project in any Debug configuration.

We're now ready to create objects in Unreal Editor based on our code. For reference, all code used on this page is included below.

Finished Code

FloatingActor.h

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

#pragma once

#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();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:
    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;

    float RunningTime;
};

FloatingActor.cpp

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

#include "QuickStart.h"
#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;

}

// 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();
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20
    RunningTime += DeltaTime;
    SetActorLocation(NewLocation);
}