2016年11月23日 星期三

Unreal: Drawing Memo

since: 2016/11/23
update: 2016/12/13

reference:
1. Widget Blueprints | Unreal Engine
2. Draw Line node in a UMG Widget - UE4 AnswerHub


A. Drawing in a UMG Widget (UI)
    1. 新增 Widget Blueprints
        a. Add New > User Interface > Widget Blueprint

        b. 取名為: MyWidgetBlueprint, 雙擊滑鼠進入

        c. 覆寫 On Paint 函式:

        d. 由 On Paint 節點, 連結出 Draw Line 節點

-----------------------------------------------------------------------------------------------

   2. Level Blueprint
       a.開啟 Level Blueprint

       b. 新增 create widget 節點

       c. 在新增的節點(名稱自動改為 Construct NONE)的 Class 屬性,
           選擇之前新增的 MyWidgetBlueprint 

       d. 新增 Get Player Controller 與 Add to Viewpoint 節點, 並作相關連結

       e. 結果: (在 VR 裡不太正常, 且是 2D 的)

-----------------------------------------------------------------------------------------------

B. Draw Debug (for debug used)
    1. 開啟 Level Blueprint:
        > 新增 Draw Debug Line 節點, 與相關的節點

    2. 結果: (3D)

-----------------------------------------------------------------------------------------------

2016年11月22日 星期二

Unreal: Spline and SplineMesh Components

since: 2016/11/22
update: 2016/11/22

reference:
1. Unreal Engine 4 Spline and SplineMesh Components - YouTube

A. 新增專案與 Blueprint Class
     1. 新增一個 Blank 的 Blueprint 專案

     2. 新增 Blueprint Class

     3. Parent Class 選擇: Actor

     4. 取名為: BP_SplineComponent

-----------------------------------------------------------------------------------------------

B. 幫 BP_SplineComponent 新增 Spline 元件
      1. 滑鼠雙擊  BP_SplineComponent
          > Add Component
          > Spline

      2. 結果:

      3. 回到遊戲關卡, 將 BP_SplineComponent 拖拉到場景裡

      4. 調整與新增 spline point

-----------------------------------------------------------------------------------------------

C. 匯入 static mesh asset
     1. Import

     2 . Import all

     3. 結果:

-----------------------------------------------------------------------------------------------

D. 取得 Spline Component 的所有 Spline Point
    1. Spline Component Overview

    2. 滑鼠雙擊  BP_SplineComponent, 換到 Construction Script 頁籤
         > 將 Spline 元件拖拉進來

     3. 新增 Get Number Of Spline Points 節點

     結果:

     4. 新增 For Loop 節點:

     結果:

     5. 設定 For Loop 的 First Index 與 last Index

-----------------------------------------------------------------------------------------------

E. 新增 SplineMesh Components
    1. 幫迴圈的每次動作新增 SplineMesh Component

     結果:

    2. 幫每個 SplineMesh Component 設定 起始(位置 & 斜率)與結束(位置 & 斜率) 
        > 取消勾選: Context Sensitive

     結果: (設定 "Set Start and End" 節點的 Target)

    3. 新增 "Get Location and Tangent at Spline Point" 節點

    結果:

    4. 設定 "Get Location and Tangent at Spline Point" 節點的輸入與輸出連結

    5. 準備給 "Set Start and End" 節點的 "End Pos" 與 "End Tangent" 輸入 pins:
        > 幫 For Loop 節點的 index 輸出 pin 連結 minus 節點
        > 複製 "Get Location and Tangent at Spline Point" 節點

    結果: 連結 "Set Start and End" 節點的 "End Pos" 與 "End Tangent" 輸入 pins

    6. 新增 "Set Static Mesh" 節點

    結果:

    7. 更新節點連結:
        > Construction Script 連結到 For Loop
        > Get Number Of Spline Points 的 Return Value 先減 1 再傳給 For Loop
        > 更新 "Set Static Mesh" 節點的輸入與輸出連結

    8. 備註: 調整 static mesh 面對的軸向(X or Y or Z)

-----------------------------------------------------------------------------------------------

F. 編譯並執行:

2016年11月20日 星期日

Unreal: Actor Spawning and GetDefaultObject in C++

since: 2016/11/20
update: 2016/11/20

reference:
1. Actor Spawning and GetDefaultObject | Orfeas Eleftheriou
2. I touchs: Unreal: Adding a C++ Class
3. I touchs: Unreal: Exposing C++ variables to Blueprints
4. I touchs: Unreal: Exposing C++ functions to Blueprints
5. I touchs: Unreal: Add Override C++ Event Functions in Blueprints
6. I touchs: Unreal: Create C++ Blueprint Function Library
7. I touchs: Unreal: Casting types in C++


A. 新增 "要被生成的角色" 之 C++ 類別
     1. 可以接續 I touchs: Unreal: Casting types in C++ 的專案

     2. Add New > New C++ Class...


     3. 選擇 "Actor" 作為 Parent Class > Next

     4. 幫新增的 Class 命名: UsefulActor > Create Class

-----------------------------------------------------------------------------------------------

B.  幫 UsefulActor 新增  DoSomething() 函式宣告與內容
     1. 修改: UsefulActor.h
....
    //@add ######

    UFUNCTION(BlueprintCallable, Category = "MyUsefulFunction")
    void DoSomething();
....


     2. 修改: UsefulActor.cpp
....
//@add ######

void AUsefulActor::DoSomething()
{
    GLog->Log("UsefulActor does something...");
}
....

     3. 存檔並編譯

-----------------------------------------------------------------------------------------------

C. 新增繼承自 UsefulActor 的 Blueprint 類別:
1. Add New > Blueprint Class


     2. 搜尋選擇 "UsefulActor" 作為 Parent Class > Next

     3. 命名為: BP_UsefulActor

     4. 點二下滑鼠, 進入 BP_UsefulActor 的 Blueprint,
         > 新增一個 static mesh (在此為 Sphere),
         > 主要用來被告知角色何時生成了, 並調整其大小.


-----------------------------------------------------------------------------------------------

D. 新增 "用來生成角色" 的 C++ 類別
    1. Add New > New C++ Class...


    2. 選擇 "Actor" 作為 Parent Class > Next


    3. 幫新增的 Class 命名: ActorSpawner > Create Class

-----------------------------------------------------------------------------------------------

E.  幫 ActorSpawner 新增宣告與內容
     1. 修改: ActorSpawner.h

          a. 加入
UsefulActor.h 標頭檔 (必須要在 ActorSpawner.generated.h 之前)
....
#include "GameFramework/Actor.h"
//@add ######

#include "UsefulActor.h"

#include "ActorSpawner.generated.h"
....


          b. 加入巨集定義
....
/*Blueprint Reference of UsefulActor class*/
UPROPERTY(EditDefaultsOnly,Category="ActorSpawning")
TSubclassOf<AUsefulActor> UsefulActorBP;
 
/*Delay after the Blueprint of the UsefulActor will get spawned*/
UPROPERTY(EditDefaultsOnly, Category = "ActorSpawning")
float TimeToSpawn = 2.f;
 
/*Spawns the UsefulActor Blueprint*/
UFUNCTION()
void SpawnUsefulActor();
....

 
     2. 修改: ActorSpawner.cpp
          a. 修改: AActorSpawner::BeginPlay() 函式
....
void AActorSpawner::BeginPlay()
{
    Super::BeginPlay();

    //@add ######
    FTimerHandle OutHandle;
 
    //Will call SpawnUsefulActor after the specified time
    GetWorld()->GetTimerManager().SetTimer(OutHandle, this, &AActorSpawner::SpawnUsefulActor, TimeToSpawn);
   
}
....


          a. 新增: AActorSpawner::SpawnUsefulActor() 函式
....
void AActorSpawner::SpawnUsefulActor()
{
    //If the usefulactorbp is valid
    if(UsefulActorBP)
    {
        //Spawn parameters for the current spawn.
        //We can use this for a number of options like disable collision or adjust the spawn position
        //if a collision is happening in the spawn point etc..
        FActorSpawnParameters SpawnParams;
 
        //Actual Spawn. The following function returns a reference to the spawned actor
        AUsefulActor* ActorRef = GetWorld()->SpawnActor<AUsefulActor>(UsefulActorBP, GetTransform(), SpawnParams);
 
        GLog->Log("Spawned the UsefulActor.");
    }
}

....

     3. 存檔並編譯

-----------------------------------------------------------------------------------------------

F. 新增繼承自 ActorSpawner
的 Blueprint 類別:
     1. Add New > Blueprint Class


     2. 搜尋選擇 "ActorSpawner" 作為 Parent Class > Next

     3. 命名為: BP_ActorSpawner

     4. 點二下滑鼠, 進入 BP_ActorSpawner 的 Blueprint,
          > 在 Details 裡, 將 UsefulActorBP 屬性設為: BP_UsefulActor
          (備註: 因為我們將 static mesh(Sphere) 存放在
BP_UsefulActor 裡)

-----------------------------------------------------------------------------------------------

G. 測試
    1. 將 BP_ActorSpawner 拖拉到場景.

    2. 編譯並執行:
        > 在經過 Time To Spawn 秒數後,(在此為 2 秒) 就會生成 Sphere Actor

    Output log:

-----------------------------------------------------------------------------------------------

H. 呼叫 UsefulActor 的 DoSomething 方法
    1. 修改 AActorSpawner::SpawnUsefulActor() 函式
....
//@add ######

//UsefulActorBP->DoSomething(); // ----> error
UsefulActorBP->GetDefaultObject<AUsefulActor>()->DoSomething();
....

備註: GetDefaultObject<T> comes in handy. This function, allows us to
          get the parent class of the subclass we’ve used above
          (which is where the c++ functions “reside”).

    2. 編譯並執行:
        Output log
: