C++与蓝图互调

时间:2022-06-02 14:15:23

Kismet库

蓝图方法cpp使用

例:打LOG:Print String

蓝图节点的鼠标tips:Target is Kismet System Library

#include "Runtime/Engine/Classes/Kismet/KismetSystemLibrary.h"
UKismetSystemLibrary::PrintString(this, s) //KismetSystemLibrary 继承UObject

C++打LOG

DEFINE_LOG_CATEGORY_STATIC(LogName, Log, All); //.cpp文件声明LOG。注:LogName不能重,Log是个枚举,All是个枚举
UE_LOG(LogName, Log, TEXT("abc %s"),s);//可以像Printf样打印出
DECLARE_LOG_CATEGORY_EXTERN(AAAAA, Log, All); //在.h文件声明LOG
DEFINE_LOG_CATEGORY(AAAAA);//在.cpp文件使用
#include "Engine/Engine.h"
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%s %f"), *Msg, Value));//引擎打LOG 注意-1 这个key可以用来当消息池索引

蓝图 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