unreal-c++教程-第一章:第一个c++脚本
第一个c++脚本本教程简述1.创建第一个Unreal c++ 脚本2. FirstActor的结构
2.1 FirstActor.h和FirstActor.cpp2.2 Unreal宏(macros)
2.2.1 宏UCLASS()和宏GENERATED_BODY()
3 基本函数BeginPlay()和Tick()
3.1 UE_LOG函数3.2 在Unreal Log中每一帧都输出Hello Wolrd
3.2.1 FisrtActor.cpp 代码逻辑3.2.2 在UnrealEditor 中的操作
3.2.2.1 编译c++脚本3.2.2.2 将FIrstActor放到场景中去
本教程简述
本教程的面向对象是拥有一定c++基础的同学,如果仅仅只是希望对unreal进行基本的应用,请参考
unreal-教程 的第一章到第十七章
另外,如果想要更好的实现本系列的操作,还是比较建议通关unreal-教程的第一~第十七章的内容
1.创建第一个Unreal c++ 脚本
2. FirstActor的结构
在unreal里面,c++的类默由两个部分组成,第一个部分是头文件.h,第二个部分是源文件.cpp
前者定义了函数,变量的声明,后者则是对函数的具体实现。2.1 FirstActor.h和FirstActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Actor.h"#include"FirstActor.generated.h"UCLASS()
class UNREALLABX_API AFirstActor : public AActor
{GENERATED_BODY()
public:// Sets default values for this actor's propertiesAFirstActor();
protected:// Called when the game starts or when spawned
virtual voidBeginPlay() override;
public:// Called every frame
virtual voidTick(float DeltaTime) override;};// Fill out your copyright notice in the Description page of Project Settings.#include"FirstActor.h"// Sets default values
AFirstActor::AFirstActor(){// 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 spawnedvoid AFirstActor::BeginPlay(){
Super::BeginPlay();}// Called every framevoid AFirstActor::Tick(float DeltaTime){
Super::Tick(DeltaTime);}2.2 Unreal宏(macros)
值得注意的是,我们可以看到,与传统c++项目略微有些区别的是,在unreal c++ 项目中,我们会
非常频繁的看到UCLASS().UFUNCTION()等等字段。这是Unreal自定义的c++宏,目标是为了让
unreal engine注意到我们定义的这些类。从而可以进行区分,让定义了这些宏的对象能够进一步
被引擎所控制。在往后漫长的教程中,我们会渐渐添加新的宏命令,在这里,我们仅仅对代码中
存在的宏进行解释2.2.1 宏UCLASS()和宏GENERATED_BODY()
UCLASS()用于指示该c++类为Unreal 反射系统中的一部分。并且使用UCLASS(),你可以直接使用unreal提供的内存管理(垃圾回收),不需要自己去处理c++复杂的垃圾回收机制。
unreal c++编译过程由两个阶段组成。第一个阶段,由UnrealHeadTool 读取c++的头文件,并寻找对应的Unreal 宏,然后生成必要的代码替代相应的宏。(在c++里面,我们定义的宏就是为了省掉重复性工作)。第二个阶段,这是编译这最终生成的代码。
在FirstActor.h中,我们会发现,在class 之前引入了UCLASS()。在这个过程里面,
UnrealHeaderTool将会通过搜索宏,然后会产生代码,这个代码会存放到FisrtActor.generated.h文件里面,然后,它将会替代掉GENERATED_BODY()这个宏
值得注意的是,FisrtActor.genereated.h文件通常会放在所有头文件的下面,不然是会报错的,比如下图这样
3 基本函数BeginPlay()和Tick()
当你创建一个c++的Actor时,会默认提供两个函数
一个是BeginPlay()
一个是Tick()
前者是当对象SpawanActor时默认调用,后者是每一帧调用3.1 UE_LOG函数
当我们希望在Unreal 的控制台中输出一些数据的时候,我们可以使用UE_LOG(LogTemp, Warning,TEXT("Hello world"));3.2 在Unreal Log中每一帧都输出Hello Wolrd
3.2.1 FisrtActor.cpp 代码逻辑
// Fill out your copyright notice in the Description page of Project Settings.#include"FirstActor.h"// Sets default values
AFirstActor::AFirstActor(){// 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 spawnedvoid AFirstActor::BeginPlay(){
Super::BeginPlay();}// Called every framevoid AFirstActor::Tick(float DeltaTime){
Super::Tick(DeltaTime);UE_LOG(LogTemp, Warning,TEXT("Hello world"));}3.2.2 在UnrealEditor 中的操作
3.2.2.1 编译c++脚本
3.2.2.2 将FIrstActor放到场景中去
运行
结果输出
页:
[1]