ue4 c++ 介面

來源:互聯網
上載者:User

標籤:tin   class   2.0   object   doc   oca   message   file   icc   

使用UE4介面比起普通的進階語言,要多做很多工作,是因為要相容藍圖的使用,有一些小坑需要注意,開始吧。

 

1.建立介面類

開啟UE4編輯器,與往常一樣,建立C++類,然後選擇Object繼承,然後取名字,這裡我使用工作中我的介面名,作為例子說明,“ITrackTeam”

 

然後修改繼承的類,而且你沒有看錯,就是寫2個class,一個叫UTrackTeam,一個叫ITrackTeam,類名和標頭檔名可以不一樣的,我這邊命名沒有遵守虛幻的規則,本來應該命名為“UITrackTeam”和“IITrackTeam”。

.h file

#pragma once#include "Object.h"#include "ITrackTeam.generated.h"UINTERFACE(Blueprintable)class UTrackTeam : public UInterface{    GENERATED_UINTERFACE_BODY()};class ITrackTeam{    GENERATED_IINTERFACE_BODY()public:    /* 獲得前方的跟隨對象 */    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Item")    AActor* GetPreMember();    /* 獲得跟隨目標點位置 */    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Item")    FVector GetTailLocation(int32 &param1, bool param2);};

 

.cpp file

#include "Programming.h"#include "ITrackTeam.h"UTrackTeam::UTrackTeam(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer){}

 

2.C++類實現這個介面

我有個實現介面的Actor,“AImplementInterfaceActor”,接著先引用標頭檔

#include "Interface/ITrackTeam.h"

然後繼承介面類

class PROGRAMMING_API AImplementInterfaceActor : public AActor, public ITrackTeam

實現介面,注意要在原有的方法名後面,加上“_Implementation”

virtual AActor* GetPreMember_Implementation() override;virtual FVector GetTailLocation_Implementation(int32 &param1, bool param2) override;

 

.cpp file,可以這麼洋洋洒洒的寫一寫測試語句,可以被藍圖調用的。

AActor* AImplementInterfaceActor::GetPreMember_Implementation(){    AActor *temp = nullptr;    return temp;}FVector AImplementInterfaceActor::GetTailLocation_Implementation(int32 &param1, bool param2){    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::FromInt(param1));    if (param2)    {        GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red,TEXT("True") );    }    param1 = 7;    return FVector(0.0f, 1.0f, 2.0f);}

 

3.C++中,給某個類發送介面訊息

先轉化成介面類,然後調用方法,調用的時候注意,一定要用“Execute_”+方法名,括弧內,如果是無參數的方法,直接寫目標類原有的指標,如果有參數,比如一個int32和bool,連著寫在後面就可以了。

AActor* temp = nullptr;ITrackTeam* iTemp = Cast<ITrackTeam>(this);if (iTemp){    int32 refInt = 5;    FVector TmpVector = iTemp->Execute_GetTailLocation(this, refInt, true);    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TmpVector.ToString());    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::FromInt(refInt));}

 

4.確定特定類是否實現了介面

為保證 C++ 和實現介面的藍圖類之間的相容性,使用以下代碼:

obj->GetClass()->ImplementsInterface(UItemInterface::StaticClass());

 

其他連結。

官方文檔:https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Reference/Interfaces/index.html

wiki:https://wiki.unrealengine.com/Interfaces_in_C%2B%2B

問答:https://answers.unrealengine.com/questions/250263/calling-interface-functions-in-c.html

ue4 c++ 介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.