Programming Quick Start Guide翻译


C++ Programming Guide

 |_____Programming Quick Start Guide

 |_____Introduction to C++ Programming in UE4

 |_____C++ Programming Tutorials

 |_____Managing Game Code

 |_____Development Setup

 |_____Gameplay Programming

 |_____Engine Architecture

 |_____Console Manager: Console Variables in C++

 |_____Command-Line Arguments


 |_____Blueprint Function Libraries

 |_____Unreal Build System


 |_____Coding Standard

 |_____Symbol Debugger

原文地址:Programming Quick Start Guid


Programming Quick Start


Over the course of this tutorial, we'll create a new Unreal Engine project, add a new C++ class to it, then compile the project and add an instance of our new class to a level. When we're finished, we'll be able to see an Actor we programmed in C++ moving around on screen

在本教程中,我们将创建一个Unreal Engine项目,为它添加C++新类,然后编译项目和增加一个新的类实例到关卡中,当我们做完这些,我们就可以看到在 C++程序里定义的一个Actor在屏幕上移动.


1. Essential Project Setup  必要的项目设置

    (1).Open Unreal Engine from the Launcher. TheProject Browser will appear.  从登录界面打开虚幻引擎,项目浏览器将会出现

    (2).Click on the New Project tab and then select the C++ tab. From there, select Basic Code so we have a clean starting point, and make sure "With Starter Content"  is set. We'll need to enter a project name, so we'll use "QuickStart". We can now clickCreate Project and get started

         点击New Project ,选择C++标签,从这里,选择Basic Code 因此我们有一个空的起点,确保设置了With Starter Content.我们要输入项目名字,叫QuickStart吧,然后点击Create Project ,让我们开始吧.

The Unreal Editor will now open our new project. Visual Studio will also open and load the solution file that our project has created.


2. Create a C++ Class  创建一个C++类

   (1)In the Unreal Editor, we can create a new C++ class with theAdd Code to Project command, located in the File drop-down menu.

      在虚幻编辑器,我们能通过Add Code to Project (创建C++类)来创建一个新的C++类,在UE4编辑器 菜单栏  File->Add Code to Project .

    (2).The Choose Parent Class menu will open. SinceActor is the most basic class that can exist in an Unreal Engine level, we'll use theActor class as our base.    父类列表被打开,因为Actor是最基本的类,能存在虚幻引擎关卡中,我们将用Actor类作为我们的基类.

The Name Your New Actor menu will open. For this example, let's enter the name "FloatingActor", and then clickCreate Class

    (3).The Name Your New Actor menu will open. For this example, let's enter the name "FloatingActor", and then clickCreate Class

       Name Your New Actor (命名你新建的Actor类)菜单被打开.在这个例子,我们命名为"FloatingActor",然后点击Create Class(创建类)

Now that we have created a C++ class, we can switch to Visual Studio to program it. FloatingActor.cpp will be opened for us automatically, andUnreal Engine will automatically compile and reload the code with our new class.

现在我们已经创建了一个C++类,我们切换到VS去编程,FloatingActor.cpp 将会自动地为我们打开,虚幻引擎将会自动地编译和重装载我们新建的类

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 namedFloatingActor.cpp and FloatingActor.h and will be inside the "QuickStart" project.


      !!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:
         在 FloatingActor.h,我们将在后括号和分号结束的时候添加如下代码

    float RunningTime;
    (3).Switching to FloatingActor.cpp, we'll add the following code just before the closing brace at the bottom of AFloatingActor::Tick:
         切换到 FloatingActor.cpp,在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;
     !!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. 
     !!我们上面写的代码将造成 FloatingActors上下跳动平滑,使用 RunningTime变量,我们创建了随着时间的推移来跟我们的运动联系起来。

    (4).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 Engine will automatically load our changes.
         既然我们已经完成了代码,我们要编译,通过在解决方案浏览器右击我们的项目,选择Build(生成)命令,或者在UE编辑器中选 Compile 按钮.一旦编译完成,虚幻引擎将自动的装载我们修改的代码.




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

#pragma once

#include "GameFramework/Actor.h"
#include "FloatingActor.generated.h"

class QUICKSTART_API AFloatingActor : public AActor

    // Sets default values for this actor's properties

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

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

    float RunningTime;


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

#include "QuickStart.h"
#include "FloatingActor.h"

// Sets default values
    // 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()


// Called every frame
void AFloatingActor::Tick( float DeltaTime )
    Super::Tick( DeltaTime );

    FVector NewLocation = GetActorLocation();//获取Actor的位置
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));//变化的高度
    NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20//基于原先的高度+20倍的高度变化系数
    RunningTime += DeltaTime;//运行的时间累加

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
    在虚幻编辑器中,目录C++ Classes->QuickStart,   看到右边FloatingActor没?

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

         把FloatingActor 拖到游戏窗口中,这样就创建了FloatingActor的一个实例了(等等,为什么我拖进去,游戏世界什么反映都没呢?哈哈,它其实已经在世界中了,只是它不存在实体,逻辑上存在的...)在World Outliner中可以捕捉到它的影子,它被命名为"FloatingActor1"了.它的组件和属性,全在Details面板中了.

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

         我们的FloatingActor 必须要被看到啊,不然怎么操作呢?于是我们能点击Add Component (添加组件),绿色的那个按钮.然后选一个模型,比如选Cone(圆锥体)

    (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 theDetails Panel to select "FloatingActor1 (Instance)". We can now edit theLocation 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.


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