UE5(c++)demo开发日志(1):Actor类添加

发布于:2024-06-30 ⋅ 阅读:(90) ⋅ 点赞:(0)

工具---新建c++类---选用Actor,创造出头文件(.h,用于声明变量、函数、重写父类函数等)和源文件(.cpp,涉及到具体功能实现),客户端界面成功显示出来新建类:

Actor头文件默认方法简介:

UCLASS(): ue提供的宏,告诉引擎生成类的反射数据。且UCLASS对应的类必须继承自UOBject。

class COMBATGAME_API AMyActor : public AActor :AMyActor为类的名字(A为Actor类特征字),AMyActor : public AActor表示AMyActor继承自AActor

GENERATED_BODY() :宏,表示当前类不直接使用父类的构造函数

public:当前类的共有模块,外界可以直接访问

AMyActor():当前类的构造函数

protected:当前类的受限制模块,外界不能直接访问,子类可以访问

private:私有模块,外界与子类都不可访问

virtual void Tick(float DeltaTime) override:系统方法,实时执行,DeltaTime是两次执行Tick的间隔时间

virtual void BeginPlay() override:系统方法,在运行开始时执行一次

在头文件声明好函数后需要在源文件实现,默认的都已经实现好了。

.h代码:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

UCLASS()//ue提供的宏,告诉引擎生成类的反射数据。且UCLASS对应的类必须继承自UOBject
class COMBATGAME_API AMyActor : public AActor//AMyActor为类的名字(A为Actor类特征字),AMyActor : public AActor表示AMyActor继承自AActor
{
	GENERATED_BODY()//宏,表示当前类不直接使用父类的构造函数
	
public:	//当前类的共有模块,外界可以直接访问
	// Sets default values for this actor's properties
	AMyActor();//当前类的构造函数

protected://当前类的受限制模块,外界不能直接访问,子类可以访问
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;//系统方法,在运行开始时执行一次

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;//系统方法,实时执行,DeltaTime是两次执行Tick的间隔时间

private://私有模块,外界与子类都不可访问

};

.cpp代码:

// 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;

}

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

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

}


网站公告

今日签到

点亮在社区的每一天
去签到