【UE5.1 创建C++文件】

创建完一个C++文件会形成两个脚本.h 与 .cpp

核心一句话

  • .h(Header 头文件):声明 → 告诉编译器「这个类有什么、有哪些函数 / 变量」,对外暴露接口
  • .cpp(Source 源文件):定义 / 实现 → 写函数里面具体怎么跑(逻辑代码)

规则:声明放 h,实现放 cpp;h 只写签名,不写函数体(除了 inline)

有几处需要注意

1.TSubclassOf<AActor> actorTarget; 与 AActor* actorTarget;两个都是对象类型,TSubclassOf<AActor>范围更广可以从内容浏览器中添加, AActor*只能选场景中存在的对象

2.c++中string 字段为Fstring,且UE_LOG(LogTemp, Warning, TEXT("你好啊,%s,我知道你的速度是 %f"), * str, speed);打印的时候*str前要加*

3.基础对象AActor* actor; AActor*后面要加*

以下是C++场景组件

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 复制代码
// 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"));
	}
}
相关推荐
小七在进步6 分钟前
string的使用(2)
c++
青少儿编程课堂1 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:折半搜索优化子集枚举(Meet in the Middle)解析
c++·python·算法·bfs·信息学竞赛
wo_jack2 小时前
C++ 入门笔记(十七):list 入门 —— 为什么 list 不能 sort?
数据结构·c++·面试·stl·list
青少儿编程课堂4 小时前
蓝桥杯青少组图形化 + 全国青少年信息素养大赛 + 电子学会Scratch考级:校园连连看解析
c++·python·算法·bfs·信息学竞赛
mengzhi啊4 小时前
UE5 沙漠添加植物,然后在湖泊游泳。关键词:湖泊使用蓝图的重叠事件,湖泊的碰撞偏移。所有的物体都可以使用蓝图来使用它
ue5
草莓熊Lotso4 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
David猪大卫21 小时前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
HPtiger1 天前
C++ 入门笔记:从 C 到 C++ 的核心过渡(命名空间、输入输出、缺省参数、重载、引用、内联)
c语言·c++·学习
努力努力再努力wz1 天前
【Lua 进阶系列】从 C++ 对象到 Lua 变量:一文理解 userdata、Lua 栈与元表绑定机制
c++·junit·lua
慧都产品智能化1 天前
Qt 表格升级攻略:复用已有编辑器,减少重复开发
c++·qt·编辑器·qt6.3