本文章由cartzhang编写,转载请注明出处。 所有权利保留。

文章链接:http://blog.csdn.net/cartzhang/article/details/73189272

作者:cartzhang

一、 UE4 C++ 代码自动生成流程

1. 界面点击生成C++类

图1

先调用 SMenuEntryBlock::OnClicked ,然后调用UI_COMMAND(AddCodeToProject) —-> FMainFrameActionCallbacks::AddCodeToProject(),—-> OpenAddCodeToProjectDialog来打开基类选择界面。

2. 点击Create Class按钮

图2

OnFinished(this, &SNewClassDialog::FinishClicked)

调用函数SNewClassDialog::FinishClicked()—>AddCodeToProject —>GameProjectUtils::AddCodeToProject_Internal(核心都在这里实现)

3. 头文件生成

拼接文件路径信息,GameProjectUtils::GenerateClassHeaderFile来生成头文件的内容。

FinalOutput = "

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

#pragma once

#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"

UCLASS()
class HOWTO_VTE_API AMyPawn : public APawn
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    AMyPawn();

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

public:
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

};
"

4. 生成CPP文件

GenerateClassCPPFile

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

#include "HowTo_VTE.h"
#include "MyPawn.h"

// Sets default values
AMyPawn::AMyPawn()
{
    // Set this pawn 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 AMyPawn::BeginPlay()
{
    Super::BeginPlay();
    %CURSORFOCUSLOCATION%
}

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

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

}

5. 编译

检测是否可避免全部编译工程,自动热加载,代码首次编译,RecompileModule —>

CheckForFinishedModuleDLLCompile

图3

6. 工程重新加载。

代码生成完毕。

二、 一张图

图4

一图千言

三、 文件生成模板

UE4 代码写的很精妙!

看模板头文件生成:

FString FinalOutput = Template.Replace(TEXT("%COPYRIGHT_LINE%"), *MakeCopyrightLine(), ESearchCase::CaseSensitive);
    FinalOutput = FinalOutput.Replace(TEXT("%UNPREFIXED_CLASS_NAME%"), *UnPrefixedClassName, ESearchCase::CaseSensitive);
    FinalOutput = FinalOutput.Replace(TEXT("%CLASS_MODULE_API_MACRO%"), *ModuleAPIMacro, ESearchCase::CaseSensitive);
    FinalOutput = FinalOutput.Replace(TEXT("%UCLASS_SPECIFIER_LIST%"), *MakeCommaDelimitedList(ClassSpecifierList, false), ESearchCase::CaseSensitive);
    FinalOutput = FinalOutput.Replace(TEXT("%PREFIXED_CLASS_NAME%"), *PrefixedClassName, ESearchCase::CaseSensitive);
    FinalOutput = FinalOutput.Replace(TEXT("%PREFIXED_BASE_CLASS_NAME%"), *PrefixedBaseClassName, ESearchCase::CaseSensitive);
    FinalOutput = FinalOutput.Replace(TEXT("%EVENTUAL_CONSTRUCTOR_DECLARATION%"), *EventualConstructorDeclaration, ESearchCase::CaseSensitive);
    FinalOutput = FinalOutput.Replace(TEXT("%CLASS_PROPERTIES%"), *ClassProperties, ESearchCase::CaseSensitive);
    FinalOutput = FinalOutput.Replace(TEXT("%CLASS_FUNCTION_DECLARATIONS%"), *ClassFunctionDeclarations, ESearchCase::CaseSensitive);
    FinalOutput = FinalOutput.Replace(TEXT("%BASE_CLASS_INCLUDE_DIRECTIVE%"), *BaseClassIncludeDirective, ESearchCase::CaseSensitive);

CPP文件也类似的。

其中,Tmplate得到是某个继承类的模板字符串,然后通过特定符号来进行替换,生成文件。

模板文件的路径在:UE4_source\UnrealEngine\Engine\Content\Editor\Templates\

比如测试的继承基类为Pawn,他们的模板头文件和CPP文件分别为: PawnClass.h.template 和PawnClass.cpp.template 。

可以自行打开和修改文件。

PawnClass.h.template

%COPYRIGHT_LINE%

#pragma once
%BASE_CLASS_INCLUDE_DIRECTIVE%
#include "%UNPREFIXED_CLASS_NAME%.generated.h"

UCLASS(%UCLASS_SPECIFIER_LIST%)
class %CLASS_MODULE_API_MACRO%%PREFIXED_CLASS_NAME% : public %PREFIXED_BASE_CLASS_NAME%
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    %PREFIXED_CLASS_NAME%();

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

public:
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

    %CLASS_FUNCTION_DECLARATIONS%
    %CLASS_PROPERTIES%
};

PawnClass.cpp.template

%COPYRIGHT_LINE%

#include "%MODULE_INCLUDE_PATH%"
#include "%UNPREFIXED_CLASS_NAME%.h"
%ADDITIONAL_INCLUDE_DIRECTIVES%

// Sets default values
%PREFIXED_CLASS_NAME%::%PREFIXED_CLASS_NAME%()
{
    // Set this pawn 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 %PREFIXED_CLASS_NAME%::BeginPlay()
{
    Super::BeginPlay();
    %CURSORFOCUSLOCATION%
}

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

}

// Called to bind functionality to input
void %PREFIXED_CLASS_NAME%::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

}

%ADDITIONAL_MEMBER_DEFINITIONS%

是不是可以自定义头文件呢,估计可以。暂时没有研究。

UE4 工程量太大了,什么样脑子才能架构这玩意啊,他们都是天才!


UE4Paragon游戏人物截图!

谢谢浏览,欢迎点赞!!!

UE4中类自动生成代码解析的更多相关文章

  1. 【MyBatis】MyBatis自动生成代码查询之爬坑记

    前言 项目使用SSM框架搭建Web后台服务,前台后使用restful api,后台使用MyBatisGenerator自动生成代码,在前台使用关键字进行查询时,遇到了一些很宝贵的坑,现记录如下.为展示 ...

  2. 02 使用Mybatis的逆向工程自动生成代码

    1.逆向工程的作用 Mybatis 官方提供了逆向工程,可以针对数据库表自动生成Mybatis执行所需要的代码(包括mapper.xml.Mapper.java.pojo). 2.逆向工程的使用方法 ...

  3. 使用mybatis-generator-core自动生成代码

    SSM框架可以使用mybatis-generator-core-1.3.2.jar来自动生成代码,以下是配置和使用的代码. generatorConfig.xml <?xml version=& ...

  4. Maven mybatis-generator自动生成代码

    mybatis-generator可以自动生成代码,不管你是否喜欢它生成的代码的风格,它确实有助于我们更快速便捷的生成代码. Maven pom文件配置: <build> <plug ...

  5. springboot mybatis 自动生成代码(maven+IntelliJ IDEA)

    1.在pom文件中加入需要的依赖(mybatis-generator-core) 和 插件(mybatis-generator-maven-plugin) <dependency> < ...

  6. IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)

    IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...

  7. (转)MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码

    http://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sql,那么 ...

  8. 使用Mybatis的逆向工程自动生成代码

    1.逆向工程的作用 Mybatis 官方提供了逆向工程,可以针对数据库表自动生成Mybatis执行所需要的代码(包括mapper.xml.Mapper.java.pojo). 2.逆向工程的使用方法 ...

  9. MyBatis框架之mybatis逆向工程自动生成代码

    http://www.jb51.net/article/82062.htm Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们 ...

随机推荐

  1. 团队项目系列博客 —— 在路上(之wampserver 修改根目录以及配置多站点以及修改端口号)

    团队项目系列博客 -- 在路上(之wampserver 修改根目录以及配置多站点以及修改端口号) 标签(空格分隔): wampserver php 参考:参考文献1.慕课网.知乎.github 一.w ...

  2. ipvs+ldirectord实现高可用ipvs

    一.heartbeat准备 1.接上文 2. 安装heartbeat-ldirectord组件包 [root@node1 heartbeat]# -.el6.x86_64.rpm 3.ldirecto ...

  3. Implement JSON Web Tokens Authentication in ASP.NET Web API and Identity 2.1 Part 3 (by TAISEER)

    http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-an ...

  4. python sort dict 总结

    python中的dict是不能排序的,只有对dict的representation进行排序,例如list或者tuple 排序肯定会用到sorted函数,那么我们就来讲一下sorted函数. sorte ...

  5. 解决"此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站"的问题

    在ASP.NET MVC项目中,使用AJAX向控制器发送GET请求获取JSON数据时,出现这个错误:"此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站.若要允许 G ...

  6. Zookeeper Zkclient客户端

    Zkclient是对Zookeeper的原生API进行了包装,实现了超时重连.Watcher反复注册等功能,它可以实现递归创建,删除节点,但是zkClient不能递归给节点赋值. 主要的api如下: ...

  7. monkey测试小记

    本篇中不记录环境搭建,只是介绍一些经验和小秘诀吧. 一.使用安卓模拟器进行测试. 在刚刚接触到monkey测试的时候,用的真机进行测试,点击几万次甚至更多的时候,发现系统变慢了.也许是错觉,但是系统经 ...

  8. Generator 函数的语法

    简介 § ⇧ 基本概念 Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同.本章详细介绍 Generator 函数的语法和 API,它的异步编程应用请看< ...

  9. 【Python】模块学习之Timer定时任务,递归定时自调获取博客浏览量

    Timer定时任务 下面是Timer函数的官方doc介绍信息 """ Call a function after a specified number of second ...

  10. led,key通用IO的端口

    1 注意通用IO端口, GPBCON 只能控制一个GPBDAT位(对应的位),而GPBUP可以使能GPBCON.