C++与蓝图互调

蓝图 C++ 互调

BlueprintCallable

UFUNCTION(BlueprintCallable, Category = "test")
void SendMsg(FString msg);//供蓝图调用的C++函数

BlueprintImplementableEvent

UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "ReceiveEvent"))
void ReceiveEvent(const FString& message);//蓝图实现函数供C++调用

参考Actor BeginPlay:
meta=(DisplayName="BeginPlay")
void ReceiveBeginPlay
在C++ BeginPlay里调用ReceiveBeginPlay

BlueprintPure

UFUNCTION(BlueprintPure, Category = "TAMediator") //蓝图输出 绿色

BlueprintNativeEvent

UFUNCTION(BlueprintNativeEvent) //本函数可用C++或蓝图实现
void fun1();//蓝图覆写函数
virtual void fun1_Implementation();//UHT生成 c++要重写的函数

C++实现蓝图接口

UINTERFACE(Category = "My Interface", BlueprintType, meta = (DisplayName = "My Interface"))
class MYMODULE_API UMyInterface : public UInterface {
	GENERATED_UINTERFACE_BODY()
};

class MYMODULE_API IMyInterface {
    GENERATED_IINTERFACE_BODY()
public:
    UFUNCTION(BlueprintCallable)
    virtual void fun1();//Error
    
    UFUNCTION(BlueprintCallable)
    void fun1();//Error

    UFUNCTION(BlueprintNativeEvent)
    void fun1();//蓝图可覆写

    /// My Initialization Interface.
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
    void OnInitialized(const AMyActor* Context);//  蓝图可覆写可调用

    UFUNCTION(BlueprintImplementableEvent)
    void Death();//True  在蓝图内以事件呈现
};

C++内调用 OnInitialized:

const auto &Interface = Cast<IMyInterface>(Actor);
if (Interface) {
    Interface->Execute_OnInitialized(Actor,Context);
}
// Else, Execute Interface on Blueprint layer instead:
if (Actor->GetClass()->ImplementsInterface(UMyInterface::StaticClass())) {
    IMyInterface::Execute_OnInitialized(Actor,Context);
}

相关网址

https://wiki.unrealengine.com/index.php?title=Interfaces_in_C%2B%2B
https://blog.csdn.net/debugconsole/article/details/50454884

posted @ 2018-06-27 15:57  晨祷  阅读(2064)  评论(0编辑  收藏  举报