先创建一个项目
创建新的c++类
选择父类
此时,生成了默认代码,并自动打开了vs,现在我们讲解一下这些代码的含义。
先看头文件
//包含了一套ue编程环境的普遍常用类,类似于我们之前用的iostream
#include "CoreMinimal.h"
//该类继承了Actor类,所以我们必须要引入其头文件
#include "GameFramework/Actor.h"
//所有反射的数据存在于该文件中
#include "FloatingActor.generated.h"
注意:FloatingActor.generated.h要放在所有头文件下面
UCLASS() //这个宏是初步理解是,标记了QUICKSTART_API是一个类,能被ue识别
class QUICKSTART_API AFloatingActor : public AActor
{
GENERATED_BODY() //也是一个宏,里面有很多代码,编译时将那些代码编译进去
public:
// Sets default values for this actor's properties
AFloatingActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
再看源文件
#include "FloatingActor.h"
// Sets default values
AFloatingActor::AFloatingActor()
{
// true值代表actor每一帧都调用Tick(DeltaTime)函数
//如果设为false,就不用每帧都调用了,并且可以提高性能
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AFloatingActor::BeginPlay()
{
Super::BeginPlay(); //调用父类函数
}
// Called every frame
void AFloatingActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime); //同上
}