Files
UnrealEngineUWP/Engine/Documentation/Source/Programming/QuickStart/3/QuickStart_3.INT.udn
Matthew Clark d8e50ff856 #UE4doc #docs:
Updated Engine version
Bolded a couple terms
Replaced 'Unreal Engine' with 'Unreal Editor' in some places.

[CL 2613633 by Matthew Clark in Main branch]
2015-07-08 11:42:13 -04:00

183 lines
5.4 KiB
Plaintext

Availability: Public
Title:3 - Write and Compile C++ Code
Crumbs:%ROOT%, Programming, Programming/QuickStart
Description:Writing a C++ class and compiling it with Unreal Engine
SkillLevel: Beginner
Version: 4.9
[VAR:Steps]
[OBJECT:Navigation]
[PARAM:previous]
[Previous Step](Programming\QuickStart\2 "%Programming\QuickStart\2:title%")
[/PARAM]
[PARAM:current]
[/PARAM]
[PARAM:home]
[QuickStart Home](Programming\QuickStart)
[/PARAM]
[PARAM:next]
[Next Step](Programming\QuickStart\4 "%Programming\QuickStart\4:title%")
[/PARAM]
[/OBJECT]
[/VAR]
%Steps%
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)](SolutionExplorer_Files.png)
[REGION:note]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.[/REGION]
1. In `FloatingActor.h`, we'll add the following code just before the closing brace and semicolon at the end of the file:
float RunningTime;
1. 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);
[REGION:tip] 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. [/REGION]
%Globals:OSSelect%
[OBJECT:ToggleButtonContent]
[PARAMLITERAL:category]
OS
[/PARAMLITERAL]
[PARAMLITERAL:id]
windows
[/PARAMLITERAL]
[PARAMLITERAL:active]
active_button_content
[/PARAMLITERAL]
[PARAM:content]
* 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)](CompileFromVS.png)
(compiling from **Visual Studio** pictured above)
[![](CompileFromEditor.png)](CompileFromEditor.png)
(compiling from the **Unreal Editor** pictured above)
[/PARAM]
[/OBJECT]
[OBJECT:ToggleButtonContent]
[PARAMLITERAL:category]
OS
[/PARAMLITERAL]
[PARAMLITERAL:id]
mac
[/PARAMLITERAL]
[PARAMLITERAL:active]
active_button_content
[/PARAMLITERAL]
[PARAM:content]
[REGION:tip]
When building in Xcode, you are only compiling the game project, not the Editor.
[/REGION]
* 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 pictured above)
* 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 pictured above)
[![](CompileFromEditor.png)](CompileFromEditor.png)
(compiling from the **Unreal Editor** pictured above)
[REGION:note]
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.
[/REGION]
[/PARAM]
[/OBJECT]
----
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.
[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%