UE5.1 蓝图调用C++方法

一、Actor组件(场景组件调用)

.h头文件

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "MySceneComponent_fan.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class TEST818_API UMySceneComponent_fan : public USceneComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UMySceneComponent_fan();
	
protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="MyTest")
	int abd=0;
	float speed ;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyTest")
	FString str ;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="MyTest")
    AActor* TargetActorFan;
	void ModifyActorByTransform(AActor* TargetActor);
	UFUNCTION(BlueprintCallable)
	void TestFan(int number);

};

.cpp源文件

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "MySceneComponent_fan.h"

// Sets default values for this component's properties
UMySceneComponent_fan::UMySceneComponent_fan()
{
	PrimaryComponentTick.bCanEverTick = true;

	str = TEXT("hahahh哈");
	speed = 3.2f;
	abd = 9;
	// 加上这行测试:
	UE_LOG(LogTemp, Warning, TEXT("====== 构造函数执行了,代码已更新! ======"));
	
}


// Called when the game starts
void UMySceneComponent_fan::BeginPlay()
{
	Super::BeginPlay();
	UE_LOG(LogTemp, Warning, TEXT("999"));
	UE_LOG(LogTemp, Warning, TEXT("你好啊,%s,我知道你的速度是 %f"), * str, speed);
	// ...
	UE_LOG(LogTemp, Warning, TEXT("Hello from MySceneComponent_fan!"));
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Hello from MySceneComponent_fan!"));
	}
	if (TargetActorFan) 
	{
		ModifyActorByTransform(TargetActorFan);
	}
	//// 举例:获取当前组件所属的Actor(自己的父Actor)
	//AActor* MyOwner = GetOwner();

}


// Called every frame
void UMySceneComponent_fan::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	if (GEngine) {
		abd++;
		if (abd > 15) {

		}
		else
		{
			// ...
			TestFan(abd);
		}
		
	}
	
}
void UMySceneComponent_fan::ModifyActorByTransform(AActor* TargetActor)
{
	if (!IsValid(TargetActor)) return;

	FTransform NewTrans;
	NewTrans.SetLocation(FVector(100, 0, 50));    // 位置
	NewTrans.SetRotation(FRotator(0, 90, 0).Quaternion()); // 旋转:Yaw90度
	NewTrans.SetScale3D(FVector(1.5, 1.5, 1.5));  // 缩放

	TargetActor->SetActorTransform(NewTrans);
}
void UMySceneComponent_fan::TestFan(int number) 
{
	//UE_LOG(LogTemp, Display, TEXT("abd=%d"),number);
	if (GEngine) {
		GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Blue, TEXT("fanhahahhah"));
	}
}

二、Actor(动态对象)调用

此处以创建好的 BP_MyActorFan 为例

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

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

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

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UFUNCTION(BlueprintCallable,Category="fan")
	void SpawnActorFan();

	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="fan")
		TSubclassOf<AActor> actorTarget;
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="fan")
		FTransform tran;
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="fan")
		FString str;

};
cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyActorFan.h"

// Sets default values
AMyActorFan::AMyActorFan()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AMyActorFan::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyActorFan::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
void AMyActorFan::SpawnActorFan() 
{

	UE_LOG(LogTemp, Display, TEXT("开始生成%s"), *str);

	FActorSpawnParameters param;
	param.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
	GetWorld()->SpawnActor<AActor>(actorTarget, tran, param);
}
相关推荐
易知微EasyV数据可视化3 小时前
UE5 中接入 GDAL:从vcpkg编译到插件打包
ue5
努力学习的小廉3 小时前
UE5入门系列(一):环境搭建.md
ue5
fanfan_hongyun6 小时前
【UE5.1 创建C++文件】
c++·ue5
努力学习的小廉9 小时前
UE5 入门系列(二):编辑器基础操作速通
ue5·编辑器
Дерек的学习记录1 天前
Unreal Engine 5:开关门实现和流程控制
开发语言·学习·ue5
远离UE42 天前
拉格朗日 动力学问题
ue5·图形渲染·图形学·动力学
远离UE47 天前
UE5 Substrate Thin Film 源码拆析
开发语言·javascript·ue5
七仔啊8 天前
UE5源码下载及编译
ue5
Zhichao_978 天前
【UE5.8 MCP】接入AI大模型
ue5