Files
UnrealEngineUWP/Engine/Documentation/Source/Programming/QuickStart/4/QuickStart_4.INT.udn

117 lines
3.7 KiB
Plaintext

Availability: Public
Title:4 - Test Your Code
Crumbs:%ROOT%, Programming, Programming/QuickStart
Description:Creating a test object in your level
SkillLevel: Beginner
Version: 4.8
[VAR:Steps]
[OBJECT:Navigation]
[PARAM:previous]
[Previous Step](Programming\QuickStart\3 "%Programming\QuickStart\3:title%")
[/PARAM]
[PARAM:current]
[/PARAM]
[PARAM:home]
[Programming Home](Programming)
[/PARAM]
[PARAM:next]
[Next Step](Programming\QuickStart\5 "%Programming\QuickStart\5:title%")
[/PARAM]
[/OBJECT]
[/VAR]
%Steps%
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)](ClassInContentBrowser.png)
1. 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)](WorldOutliner.png)
1. 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)](AddStaticMesh.png)
1. 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)](SetActorLocation.png)
1. Press the **Play** button and watch the cone float up and down!
[![](MovingCone.png)](MovingCone.png)
[OBJECT:Section]
[PARAMLITERAL:id]
code
[/PARAMLITERAL]
[PARAM:heading]
Finished Code
[/PARAM]
[PARAM:content]
**FloatingActor.h**
// Copyright 1998-2015 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();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
float RunningTime;
};
**FloatingActor.cpp**
// Copyright 1998-2015 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);
}
[/PARAM]
[/OBJECT]
%Steps%