虚幻4——C++笔记

1,858 阅读3分钟

「这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战

1、头文件分析

  • CoreMinimal.h: 包含一套来自UE4核心编程环境普遍存在的类型(包括FString,FName、TArray等)文件目录位于UE_4.27\Engine\Source\Runtime\Core\Public包含多个引擎头文件。把所需最小头文件包含在一起形成一个头文件
  • GameFramework:游戏框架文件
  • Actor.h:Actor.h包含Actor的父类AActor
  • MyActor.generated.h:必须在所有头文件的下方,包含UE4的反射机制
  • UCLASS():标明AMyActor类能被虚幻四引擎识别,表示这个类要被引擎识别,并有ue4的类的反射机制,括号里还可以填相关属性
  • override:表示虚函数要重写
#pragma once

#include "CoreMinimal.h"//CoreMinimal.h: 包含一套来自UE4核心编程环境普遍存在的类型(包括FString,FName、TArray等)文件目录位于				  						`UE_4.27\Engine\Source\Runtime\Core\Public`包含多个引擎头文件。把所需最小头文件包含在一起形成一个头文件
#include "GameFramework/Actor.h"//游戏框架,Actor.h包含AMyActor的父类AActor
#include "MyActor.generated.h"

UCLASS()//UCLASS():标明AMyActor类能被虚幻四引擎识别
class COMPANY_API AMyActor : public AActor//COMPANY_API为项目名
{
	GENERATED_BODY()//UE4生成的东西
	
public:	
	// Sets default values for this actor's properties
	AMyActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;//override表示虚函数要重写

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

};
  • PrimaryActorTick.bCanEverTick: 逐帧调用Tick()
  • super: 相当于父类域名Super::BeginPlay();等价于AActor::BeginPlay();
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyActor.h"

// Sets default values
AMyActor::AMyActor()
{
 	// 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;//逐帧调用Tick()

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{	//super相当于父类域名
	Super::BeginPlay();
    //等价于
    //AActor::BeginPlay();
	
}

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

}

2、入门教程源码分析

VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));//创一个一个叫mesh的静态网格体

源码如下:一个泛型模板

template<class TReturnType>
TReturnType* CreateDefaultSubobject(FName SubobjectName, bool bTransient = false)
{
    UClass* ReturnType = TReturnType::StaticClass();//CreateDefaultSubobject的参数需要UClass类型的指针
    return static_cast<TReturnType*>(CreateDefaultSubobject(SubobjectName, ReturnType, ReturnType, /*bIsRequired =*/ true, bTransient));//类型强转为模板类型
}

声明一个静态对象,类型是ConstructorHelpers结构体内的模板类FObjectFinder,类型为UStaticMesh,对象名为CubeVisualAsset 参数为形状文件的引用 '/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'类似于目录

static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));

判断是否创建成功

if (CubeVisualAsset.Succeeded())//是否创建成功
	{
		VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
		VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));//设置相对位置
	}

获取当前actor的位置,我认为加上this更清晰明了

FVector NewLocation = this->GetActorLocation();

获取当前actor的角度,我认为加上this更清晰明了

FRotator NewRotation = this->GetActorRotation();

获取自从物体创建,游戏运行了多久

float RunningTime = this->GetGameTimeSinceCreation();

一个since函数值在-1到1之间

	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));

z轴方向移动

	NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20

每秒旋转20度,pitch绕y,yaw绕z,roll绕x轴,x:roll,翻滚角,以x为轴,进行yz平面的旋转; y:pitch,俯仰角,以y为轴 z:yaw,航向角,以z为轴

float DeltaRotation = DeltaTime * 20.0f;  
NewRotation.Yaw += DeltaRotation;//z周旋转

设置位置

this->SetActorLocationAndRotation(NewLocation, NewRotation);