UE4 C++与蓝图交互
一、C++调用蓝图的函数
本来应该有一个很好的示例,但一下却忘了适用于哪些地方,只有简单写一个步骤了。
1.新建一个C++类继承Actor,名称随意,我这里继承了一个SplineMesh,一样de,并定义一个函数表示在蓝图中可重载(同时这个函数在C++中不必实现)
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "Engine/SplineMeshActor.h" #include "MySplineMeshActor.generated.h" /** * 蓝图可继承 */ UCLASS(Blueprintable) class MYPROJECT_API AMySplineMeshActor : public ASplineMeshActor { GENERATED_BODY() public: AMySplineMeshActor(); /*此函数可以在蓝图或关卡蓝图图表内进行重载*/ UFUNCTION(BlueprintImplementableEvent, Category="SplineMesh") void PrintMessage(const FString &Message); };2.新建一个蓝图继承此类,并重载PrintMessage函数,我这里就是讲Message打印出来
3.将蓝图拖在场景中,这里的名称设置为BP_MySplineMesh_Actor,后面方便查询
4.在C++中找到这个物体并调用PrintMessage函数
void AMyPlayerController::BeginPlay() { TArray<AActor*> _TempArryActors; UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::GetClass(), _TempArryActors); for (int32 i = 0; i < _TempArryActors.Num(); i++) { if (_TempArryActors[i]->GetName() == FString("BP_MySplineMesh_Actor")) { Cast<AMySplineMeshActor>(_TempArryActors[i])->PrintMessage(TEXT("找到Actor&调用PringMessage函数")); } } }5.运行截图