介绍Unreal Engine 4中的接口(Interface)使用C++和蓝图

这个教程是从UE4 Wiki上整理而来.git

在C++中直接使用Interface你们应该很熟悉。只是简单先定义一个个有虚函数的基类,而后在子类中实现相应的虚函数。像这样的虚函数的基类通常概念上叫接口。那接下来看看UE4中怎样在C++中定义接口的。github

.h编辑器

#pragma once
#include "TargetInterface.generated.h"
UINTERFACE(MinimalAPI)
class UTargetInterface :
    public UInterface
{
    GENERATED_UINTERFACE_BODY()
};
 
class ITargetInterface{
    GENERATED_IINTERFACE_BODY()
public:
    UFUNCTION(BlueprintImplementableEvent, meta=(FriendlyName = "On Interact"))
    void OnInteract(bool bNewOpen);
    virtual float GetHealth();
 
};

.cppide

#include "YourProject.h"
#include "TargetInterface.h"
 
UTargetInterface::UTargetInterface(const FObjectInitializer& ObjectInitializer) 
    : Super(ObjectInitializer)
{
 
}
// Give GetHealth a default implementation
float ITargetInterface::GetHealth(){
    return 0.0f;
}

首先定义一个UTargetInterface这个是给引擎内部模块用的,通常不会用到,可是要定义。函数

ITargetInterface是你要使用的类,你要声明的函数接口都放在这里面。若是是定义函数给C++用的,那么你就直接按标准的C++声明一个虚函数就OK了,而后你要实现这个接口的子类继承ITargetInterface实现GetHealth函数就能够了。若是是给Blueprint用的,就要在函数前一行声明的UFUNCTION内加上BlueprintImplementableEvent,这样蓝图就能够实现这个接口了,蓝图中就像普通事件同样实现。ui

.hspa

#pragma once

#include "GameFramework/Actor.h"
#include "TargetInterface.h"
#include "NPCActor.generated.h"

UCLASS()
class MMWORLD_API NPCActor
    : public AActor
    , public ITargetInterface
{
    GENERATED_BODY()
public:
    AInteractiveTargetActor();
virtual float GetHealth() override;
protected:
    float Health;
};

 

.cppcode

#include "YourProject.h"
#include "NPCActor.h"

NPCActor::NPCActor()
{
   Health = 100.0f;
}

float NPCActor::GetHealth()
{
   return Health;
}
 

在C++代码中这样调用.若是是你知道Actor类的实际类型,直接调用就能够了。你不知道Actor的类型时候Interface才有实际的用处。对象

auto MyInterface = Cast<ITargetInterface>(ActorInstance);
if (MyInterface)
{
   float ActorHealth = MyInterface->GetHealth();
}

可是这样的接口蓝图中是没法使用的,蓝图彻底没法知道它的存在。你能够再用Blueprint Function Library的方式再包装一层,可是我以为这不是很好。blog

在蓝图中实现Event接口:

以前的NPCActor由于已经继承了ITargetInterface因此,你在蓝图编辑器里能够直接实现OnInteract接口事件。

那若是你纯蓝图的Actor怎么办。

进入蓝图编辑点Class Settings

3402568203

在Interfaces面板点Add加入Target Interface就行了

3523847589

 

那怎调用这个蓝图实现的接口在C++里?

看下面的代码

if (ActorInstance->GetClass()->ImplementsInterface(ITargetInterface::StaticClass()))
{
  ITargetInterface::Execute_OnInteract(ActorInstance, true);
}
ActorInstance->GetClass()->ImplementsInterface(ITargetInterface::StaticClass())) 是用来判断这个Actor是否实现了TargetInterface,无论是在蓝图仍是C++中均可以正确的判断(可是只有BlueprintImplementableEvent和BlueprintNativeEvent(这个后面再介绍)的函数才能够被蓝图实现)。Execute_OnInteract执行蓝图事件,第一个参数是UObject对象(接口调用的执行目标对象),后面的参数就是OnInteract函数的参数。这个函数是UHT自动生成的。其实这个函数本质上就是调用的UObject的ProcessEvent,具体你能够看生成的代码。(你到这个目录下的文件看看就知道了,Yourproject\Intermediate\Build\Win64\Inc\Yourproject\Yourproject.generated.cpp,Yourproject是你的项目名称)
 

最后一个问题,那若是想要这个函数接口既能够被C++实现又要被Blueprint实现怎么办?

你只要简单的把BlueprintImplementableEvent改为BlueprintNativeEvent就能够了。

.h

#pragma once

#include "InteractionsInterface.generated.h"

UINTERFACE()
class MMWORLD_API UInteractionsInterface : public UInterface
{
    GENERATED_UINTERFACE_BODY()
};

class MMWORLD_API IInteractionsInterface
{
    GENERATED_IINTERFACE_BODY()
public:
    UFUNCTION(BlueprintNativeEvent)
    void SwitchTurned(bool bNewOnOrOff, int32 CustomParam);
};

.cpp

#include "MMWorld.h"
#include "InteractionsInterface.h"

UInteractionsInterface::UInteractionsInterface(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{

}

实现的Actor

.h

#pragma once

#include "GameFramework/Actor.h"
#include "InteractionsInterface.h"
#include "InteractiveTargetActor.generated.h"

UCLASS()
class MMWORLD_API AInteractiveTargetActor
    : public AActor
    , public IInteractionsInterface
{
    GENERATED_BODY()
public:
    AInteractiveTargetActor();

    virtual void SwitchTurned_Implementation(bool bNewOnOrOff, int32 CustomParam) override;

    UFUNCTION(BlueprintImplementableEvent, Category = Interactive)
    void SwitchAllTurnedOnOrOff(bool bOnOrOff);

    UFUNCTION(BlueprintCallable, Category = Interactive)
    bool IsSwitchTurned(int32 Index);

    UFUNCTION(BlueprintCallable, Category = Interactive)
    bool IsSwitchAllTurnedOn();

    UFUNCTION(BlueprintCallable, Category = Interactive)
    bool IsSwitchAllTurnedOff();

protected:
    UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (ClampMin = "1", ClampMax = "32", UIMin = "1", UIMax = "32"))
    int32 NeedSwitchTurnedNum;

    uint32 SwitchTurnedStates;
};

SwitchTurned在C++中要实现的函数是virtual void SwitchTurned_Implementation(bool bNewOnOrOff, int32 CustomParam) override; (BlueprintNativeEvent的函数名字_Implementation)。这个函数也是UHT自动生成的。

若是是你在Blueprint中是实现了SwitchTurned接口,那么C++的实现就会被覆盖隐藏掉。

那若是你的蓝图又要掉用C++接口实现怎么办?

UE4中没有直接实现这样的机制。可是你能够把SwitchTurned_Implementation实现提取出来一个新函数,而后把这个函数定义成UFUNCTION(BlueprintCallable, Category = Interactive),而后在蓝图中调用这个函数就解决了。

 

好了就介绍到这里,第一次写关于UE4的东西,不对的地方但愿你们指正。但愿这个对你有帮助。

参考例子:

https://github.com/henrya2/MMWorld

 

参考文章:

相关文章
相关标签/搜索