【UE4】 C++ FunctionLibrary调用蓝图以及常见问题

2021-11-10 14:53:25 浏览数 (1)

BP_Actor.h

代码语言:javascript复制
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyMccActor.generated.h"

UCLASS()
class MYCPPPROJECT_API AMyMccActor : public AActor
{
    GENERATED_BODY()
    
public: 
    // Sets default values for this actor's properties
    AMyMccActor();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public: 
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    UFUNCTION(BlueprintImplementableEvent, Category = "SplineMesh")
    void PrintMessage(const FString &Message);


};

Othre.cpp

代码语言:javascript复制
void XXX::BeginPlay()
{
  TArray _TempArryActors;
  UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::GetClass(), _TempArryActors);
  for (int32 i = 0; i < _TempArryActors.Num(); i  )
  {
    if (_TempArryActors[i]->GetName() == FString("AMyMccActor"))
  {
   Cast(_TempArryActors[i])->PrintMessage(TEXT("找到Actor&调用PringMessage函数"));
  }
}

自定义节点传参时注意要用常量,否则蓝图中无法调用节点

代码语言:javascript复制
UFUNCTION(BluePrintImplementableEvent)
	void POIRunEvent(const TArray &POIDataOut);

通过GetAllActorClass获取实例时UE4自身BUG会误报红,在FunctionLibrary中的GetWrold需要替换GetGameInstance()

代码语言:javascript复制
void UMultiThreadFunctionLibrary::SearchPOIActor()
{
	//通过 UGameplayStatics::GetAllActorsOfClass 查找
		/*
		*获取类 AActor::GetClass() 换成获取静态类 AActor::StaticClass()
		*GetAllActorsOfClass会爆红是UE4 BUG 可以忽略
		*在FunctionLibrary中使用GetWorld()换成GWorld->GetGameInstance()
		*/
	UGameplayStatics::GetAllActorsOfClass(GWorld->GetGameInstance(), APOIExecActor::StaticClass(), _TempArryActors);
}

获取实例中的函数时用Cast转换类型即可

代码语言:javascript复制
//转换APOIExecActor类型后执行其中的POIRunEvent事件
	Cast(_TempArryActors[0])->POIRunEvent(POIDataOut);

0 人点赞