找回密码
 立即注册
查看: 323|回复: 0

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

[复制链接]
发表于 2022-7-16 10:59 | 显示全部楼层 |阅读模式
第一个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放到场景中去





本教程简述
  1. 本教程的面向对象是拥有一定c++基础的同学,如果仅仅只是希望对unreal进行基本的应用,请参考
  2. unreal-教程 的第一章到第十七章
  3. 另外,如果想要更好的实现本系列的操作,还是比较建议通关unreal-教程的第一~第十七章的内容
复制代码
1.创建第一个Unreal c++ 脚本





2. FirstActor的结构
  1. 在unreal里面,c++的类默由两个部分组成,第一个部分是头文件.h,第二个部分是源文件.cpp
  2. 前者定义了函数,变量的声明,后者则是对函数的具体实现。
复制代码
2.1 FirstActor.h和FirstActor.cpp
  1. // 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()
  2. class UNREALLABX_API AFirstActor : public AActor
  3. {GENERATED_BODY()
  4. public:// Sets default values for this actor's propertiesAFirstActor();
  5. protected:// Called when the game starts or when spawned
  6.         virtual voidBeginPlay() override;
  7. public:// Called every frame
  8.         virtual voidTick(float DeltaTime) override;};
复制代码
  1. // Fill out your copyright notice in the Description page of Project Settings.#include"FirstActor.h"// Sets default values
  2. AFirstActor::AFirstActor(){// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  3.         PrimaryActorTick.bCanEverTick = true;}// Called when the game starts or when spawnedvoid AFirstActor::BeginPlay(){
  4.         Super::BeginPlay();}// Called every framevoid AFirstActor::Tick(float DeltaTime){
  5.         Super::Tick(DeltaTime);}
复制代码
2.2 Unreal宏(macros)
  1. 值得注意的是,我们可以看到,与传统c++项目略微有些区别的是,在unreal c++ 项目中,我们会
  2. 非常频繁的看到UCLASS().UFUNCTION()等等字段。这是Unreal自定义的c++宏,目标是为了让
  3. unreal engine注意到我们定义的这些类。从而可以进行区分,让定义了这些宏的对象能够进一步
  4. 被引擎所控制。在往后漫长的教程中,我们会渐渐添加新的宏命令,在这里,我们仅仅对代码中
  5. 存在的宏进行解释
复制代码
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()
  1. 当你创建一个c++的Actor时,会默认提供两个函数
  2. 一个是BeginPlay()
  3. 一个是Tick()
  4. 前者是当对象SpawanActor时默认调用,后者是每一帧调用
复制代码
3.1 UE_LOG函数
  1. 当我们希望在Unreal 的控制台中输出一些数据的时候,我们可以使用
复制代码
  1. UE_LOG(LogTemp, Warning,TEXT("Hello world"));
复制代码
3.2 在Unreal Log中每一帧都输出Hello Wolrd

3.2.1 FisrtActor.cpp 代码逻辑
  1. // Fill out your copyright notice in the Description page of Project Settings.#include"FirstActor.h"// Sets default values
  2. AFirstActor::AFirstActor(){// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  3.         PrimaryActorTick.bCanEverTick = true;}// Called when the game starts or when spawnedvoid AFirstActor::BeginPlay(){
  4.         Super::BeginPlay();}// Called every framevoid AFirstActor::Tick(float DeltaTime){
  5.         Super::Tick(DeltaTime);UE_LOG(LogTemp, Warning,TEXT("Hello world"));}
复制代码
3.2.2 在UnrealEditor 中的操作

3.2.2.1 编译c++脚本



3.2.2.2 将FIrstActor放到场景中去


运行


结果输出

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-1 06:56 , Processed in 0.135908 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表