Language:
Page Info
Skill Level:
Engine Version:

4 - Test Your Code

  1. In the Unreal Editor, locate the Content Browser, and expand the folder called "C++ Classes". Within that folder, there is a "QuickStart" folder that contains our Actor class, FloatingActor.

    ClassInContentBrowser.png

  2. We can drag the FloatingActor class directly into the Level Editor window to create an instance of FloatingActor in our world. It will be selected in the Level Editor and the World Outliner, where it will be called "FloatingActor1". Its Components and other properties will be visible in the Details Panel.

    WorldOutliner.png

  3. Our FloatingActor needs to be visible in the game. While it is selected, we can click Add Component in the Details Panel, and select Cone to give it a simple visual representation.

    AddStaticMesh.png

  4. Now that our customized Actor is ready, let's move it to somewhere prominent. We can select it and drag it around in the world with the left mouse button, or we can move it manually. To move it manually, we need select it in the Level Editor or World Outliner and then use the Details Panel to select "FloatingActor1 (Instance)". We can now edit the Location field of FloatingActor1's Transform directly. Let's set X to -200 and Z to 200. This will place"FloatingActor1" right over the table in our scene.

    SetActorLocation.png

  5. Press the Play button and watch the cone float up and down!

    MovingCone.png

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);
}