unreal-c++教程-第一章:第一个c++脚本

本教程简述

本教程的面向对象是拥有一定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.

#pragma once

#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 properties
	AFirstActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void Tick(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 spawned
void AFirstActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void 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()

  1. UCLASS()用于指示该c++类为Unreal 反射系统中的一部分。并且使用UCLASS(),你可以直接使用unreal提供的内存管理(垃圾回收),不需要自己去处理c++复杂的垃圾回收机制。

  2. unreal c++编译过程由两个阶段组成。第一个阶段,由UnrealHeadTool 读取c++的头文件,并寻找对应的Unreal 宏,然后生成必要的代码替代相应的宏。(在c++里面,我们定义的宏就是为了省掉重复性工作)。第二个阶段,这是编译这最终生成的代码。

  3. 在FirstActor.h中,我们会发现,在class 之前引入了UCLASS()。在这个过程里面,
    UnrealHeaderTool将会通过搜索宏,然后会产生代码,这个代码会存放到FisrtActor.generated.h文件里面,然后,它将会替代掉GENERATED_BODY()这个宏

  4. 值得注意的是,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 spawned
void AFirstActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void 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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值