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);
}
相关推荐
mengzhi啊9 小时前
UE5 沙漠添加植物,然后在湖泊游泳。关键词:湖泊使用蓝图的重叠事件,湖泊的碰撞偏移。所有的物体都可以使用蓝图来使用它
ue5
Zhichao_977 天前
【UE5.8】VS2026安装
ue5
DolitD8 天前
UE像素流与云渲染技术方案对比分析
3d·云原生·ue5·云计算·图形渲染
Дерек的学习记录9 天前
Unreal Engine 5:第三人称运动系统和人物动画
开发语言·学习·ue5
努力学习的小廉12 天前
UE5 入门系列(三):核心框架类全解析
ue5
平行云13 天前
建筑BIM/3D可视化轻量级实时评审与分享方案
unity·ue5·3dsmax·实时云渲染·云桌面·云游戏·建筑bim
BugPhysician18 天前
影之刃零首发UE崩溃排查:着色器缓存、虚拟内存与驱动版本
ue5·影之刃零·影之刃零报错·影之刃零进不去·ue崩溃·ue引擎崩溃·ue5引擎崩溃
电子云与长程纠缠20 天前
UE5魂系游戏开发教程05
开发语言·学习·游戏·ui·ue5
易知微EasyV数据可视化20 天前
UE5 中接入 GDAL:从vcpkg编译到插件打包
ue5