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

 |_____Assertions

 |_____Blueprint Function Libraries

 |_____Unreal Build System

 |_____Plugins

 |_____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.

虛幻編輯器將被打開,VS也會打開然後爲我們創建的項目加載解決方案文件


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.

      在VS中,我們用解決方案資源管理器面板來找到我們創建的C++文件,在這個例子中,他們被命名爲FloatingActor.cppFloatingActor.h,在"QuickStart"這個項目中

      !!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 ofAFloatingActor::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;
SetActorLocation(NewLocation);
     !!The code we've just written will cause FloatingActors to bob up and down smoothly, using theRunningTime 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 theSolution Browser and selecting the Build command, or by pressing theCompile button in the Unreal Editor. Once the compile succeeds,Unreal Engine will automatically load our changes.
         既然我們已經完成了代碼,我們要編譯,通過在解決方案瀏覽器右擊我們的項目,選擇Build(生成)命令,或者在UE編輯器中選Compile 按鈕.一旦編譯完成,虛幻引擎將自動的裝載我們修改的代碼.
或者

注意:根據我查閱的資料和理解,當我們添加新的函數的時候,需要關掉UE編輯器,VS重新編譯,運行.但是,我們僅僅只是修改了函數內容的話,我們可以用熱編譯,即點擊十分好用,方便!


完整的代碼如下:

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();//獲取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;//運行的時間累加
    SetActorLocation(NewLocation);//設置Actor的位置
}


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.

      既然我們的Actor已經準備好了,讓我們移動到一些合適的地方,我們可以在世界中拖拉,(w位置變幻,e旋轉變幻,r縮放變幻,空格鍵爲切換鍵),也可通過Details面板調:

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

         按Play按鈕,觀察圓錐體上升,下降.

 




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章