C++和蓝图联动(非全局)

**

一、蓝图调用.cpp函数

**
在这里插入图片描述

.h文件

public:
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
	
UFUNCTION(BlueprintCallable, Category = "MyOpenDoor")//被蓝图调用的特性	,可以在蓝图函数列表中搜索到
void TestFuncFromCPlusPlus();

.cpp文件

void UOpenDoor::TestFuncFromCPlusPlus()
{
    
    
	UE_LOG(LogTemp, Warning, TEXT("TestFuncFromC++!"));
}

二、.cpp函数调用蓝图

1.把.h文件中的UCLASS设置为蓝图可以调用, 添加Blueprintable。

UCLASS(Blueprintable, ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))

2.把c++类转为蓝图
在这里插入图片描述
**a.通过c++脚本创建蓝图。
蓝图如下:在这里插入图片描述

b.之后把蓝图挂载在场景中的物体上。**

3.在.h文件中添加

#include "Misc/OutputDeviceDebug.h"//引用输出设备Debug的源文件库

4.在.cpp文件中

// Called when the game starts
void UOpenDoor::BeginPlay()
{
    
    
	Super::BeginPlay();	

	FString cmd = FString::Printf(TEXT("TestFuncFromBP ABCDEF"));	
	FOutputDeviceDebug device;
	//CallFunctionByNameWithArguments通过参数名回调函数
	CallFunctionByNameWithArguments(*cmd,device,NULL,true);

}

猜你喜欢

转载自blog.csdn.net/weixin_43834973/article/details/142312657