UE C++·TomLooman的教程学习笔记
Tom Looman:Professional Game Development in C++ and Unreal Engine | 官网 https://courses.tomlooman.com/p/unrealengine-cpp
视频对应的项目:https://github.com/tomlooman/ActionRoguelike/commits/UE4-Lecture29-FinishedProject/
虚幻引擎 C++ 完全指南 by Tom Looman
我的项目: https://github.com/1ronName/ActionRoguelike
命名规范
https://github.com/Allar/ue5-style-guide
中文:https://github.com/thejinchao/ue5-style-guide
Unreal Gameplay Framework C++ 指南 https://tomlooman.com/unreal-engine-gameplay-framework/
为了让教程学习更顺畅,采用了旧的 Input Action/Axis Mappings。之后自己做demo还是用Enhanced Input Actions 和 Input Mappings Contexts。
Kismet是UE3中 Blueprint 的名字。
在构造函数中进行的事件绑定可能需要重启才能生效。所以不如在BeginPlay里绑定。
为了避免issue,应避免使用hot reload。不使用hotreload:关闭Editor,在VS中编译。
绑定事件需要函数声明前有UFUNCTION()
PostInitializeComponents在构造函数和BeginPlay之间执行。
由于UFUNCTION(BlueprintNativeEvent),在C++实现时需要在函数名加上_Implementation后缀。
调用接口函数:
1 | if (HitActor->Implements<USGameplayInterface>()) //是U前缀!这是接口暴露给UE的 |
function可以有返回值;而event可以绑定到timer上。
TEXT()是一个宏,可以接受更多类型的字符,可能是Unicode?UE_LOG有类似 printf 的 格式化字符串 用法。
DrawDebugString(),位于<DrawDebugHelper>中,一般TestBaseActor填nullptr,使得正常在TextLocation生成。
父类的构造函数自动调用。
SetTimer 的设计逻辑是:如果传入的 FTimerHandle 当前已激活(即已经有一个倒计时在跑),引擎会先清除(Clear)旧的计时器,然后用新的参数(延迟时间、回调函数)创建一个全新的计时器。
是否会触发其他SphereComp的Overlap,还要看本身有没有勾选 Generate Overlap Events。
断言 ensure
ensure(),UE的macro,括号中条件不满足时assert。只触发一次,类似断点,在VS中标出(还得是调试运行,在重启调试前只能触发一次)。ensureAlways()会一直触发,可以用附加到进程来调试。 不影响打包出的游戏。
check()会停下游戏,而且无法继续。
DebugGame Editor 配置编译 不会使用优化,可以用于避免优化导致的某些断点没有触发。
ensureMsgf():
1 | if (ensureMsgf(BehaviorTree, TEXT("Behavior Tree is nullptr! Please assign BehaviorTree in your AI Controller."))) |
UPROPERTY 关键字
1 | // EditAnywhere - edit in BP editor and per-instance in level. |
String 类型
FString
// Most commonly used for debugging
// Allows string 操作 manipulation (append, split, .)
FName
// Hashed for faster string comparisons.
// Used by system and gameplay logic (eg. GetMuzzleLocation(FName)
// Doesn’t change once assigned.
FText
- 用于在UI中显示的前端文本
- 易于“本地化”为不同的语言
让UI获取Gameplay中的数据变化(事件),而不是让PlayerCharacter去修改UI。
Widget的Construct在被添加到viewport后执行;Pre Construct在编辑器中就执行
从CPU到GPU的通信是比较昂贵的(用蓝图修改material的参数)
material instance 的parameter可以在运行时修改,而且结束运行时值会保存(非常方便调整效果的功能)。
创建C++类时的自动recompile可以关闭。(谁会用刚创建的空的类啊!
创建了 Material Function 记得勾选 Expose to library。
Widget中的image可以设置图片,也可以设置Material。
AI
AIModule; AIModule 和 GameplayTasks
AIController, BehaviorTree(也有其他方案, BlackBoard(存数据.
环境查询系统 Environment Querying System, EQS,可以让AI的移动目的更灵活。实际是在一块区域内 按照规则生成一组点,给这些点计算分数,选取一个或多个满意的点。能避免多个AI敌人像火车一样追逐玩家。和EnvQuery搭配的有 QueryContext,就是添加自己在EnvQuery中想要使用的变量。
EQSTestingPawn 可以用于在编辑时显示EnvQuery的测试点。
Visual Logger可以记录ai的行为,比如EnvQuery,规划的路径等等。
Observer aborts: None,self,LowerPriority,Both(待补充)
创建自己的的静态函数库,继承自UBlueprintFunctionLibrary
meta specifier,和Catergory之类的一样,写在UFUNCTION里,大多是和编辑器有关的。
game mode base: StartPlay(而不是BeginPlay)
user widget : NativeTick
和模块有关的两个文件
.uproject 和 .Build.cs 。若.uproject是只读的,手动在.Build.cs里添加即可
调试指令
呼出控制台使用 slomo 指令可以调整时间速度。
运行时按下左上角的~键。
1 | // 在SCharacter.h中 |
UE甚至自带AActor和CheatManager有关的属性。
用于调试的Console Variables
CVar
用例1
1 | // 在SGameModeBase.cpp中 |
1 | // 在 SAttributeComponent.cpp 中 |
蓝图会把 输入参数中使用引用&的放到 输出引脚,使用const保证参数是输入参数。
ActionSystem
GameplayAbilitySystem 的学习曲线比较陡峭,有下列参考资料:
Official Docs:https://docs.unrealengine.com/en-US/Gameplay/GameplayAbilitySystem/index.html
Community Docs: https://github.com/tranek/GASDocumentation // 比官方的详细得多
GameplayTags (https://docs.unrealengine.com/en-US/Gameplay/Tags/index.html)
ActionRPG Sample Game (https://docs.unrealengine.com/en-US/Resources/SampleGames/ARPG/index.html
本教材做GAS青春版,可以接触到一些GAS自动处理的部分。
“Outer会在通过NewObject<T>创建action(继承自UObject)时设置”
GameplayTags
Using Gameplay Tags to Label and Organize Your Content in UE4
https://www.unrealengine.com/en-US/tech-blog/using-gameplay-tags-to-label-and-organize-your-content-in-ue4
GameplayTags Docs: https://docs.unrealengine.com/en-US/Gameplay/Tags/index.html
ActionRPG Sample Game: https://docs.unrealengine.com/en-US/Resources/SampleGames/ARPG/index.html
记得在Build.cs里加GameplayTags模块,然后右键.uproject来生成项目文件
不能直接创建 ECollisionChannel,用TEnumAsByte<ECollisionChannel> CollisionChannel;
网络复制
属性复制:从server同步到client。(不可以反过来)
RPC:remote procedure calls。三种类型:SERVER, CLIENT, NetMulticast;
SERVER 是 Client 调用 Server;CLIENT 是 Server 调用特定 Client;NetMulticast 是 Server 调用全部 Client。
UFUNCTION(Server, Reliable)
Reliable 在没收到确认时会重发;而 Unraliable 可能丢失。
在 C++ 里实现 UFUNCTION(Server/Client/NetMulticast) 时,函数定义必须加 _Implementation 后缀。
但 声明处和调用处都不加。
TickComponent在Server和Client运行。
UPROPERTY(Replicated)
RepNotify, 在C++中是ReplicatedUsing,UPROPERTY(ReplicatedUsing="OnRep_function")
PlayerController只在Server和拥有它的Client上存在,而PlayerState在Server和所有Client上存在。
GameMode只在Server上存在。GameState则都存在。Pawn都存在。
Actor SetReplicates(true)后,会从Server复制到Client,但不会从Client复制到Server。
有用、持续的状态信息,用 RepNotify 。这样后加入服务器的玩家也能有正确的同步信息。
RPC+Unrealiable拿来传递装饰性信息,丢包了也没事的那种。
Relevancy,距离过远的Actor不复制和RepNotify (属性复制)。Reliable 多播默认被 Relevancy 限制,但可以开启选项绕过;Unreliable 多播始终受 Relevancy 限制。
AI在Server运行。
SetReplicates(true) ,AActor 的方法,设置 Actor 本身是否参与网络复制;
SetIsReplicatedByDefault(true),UActorComponent 的方法,设置 组件本身是否默认参与网络复制,通常只在构造函数中调用
使用replicateSubobject时,uobject的outer被设置为actor(而不是原本的ActorComponent)。
RepNotify 是 在网络包发送到Client时 通过比较属性,决定是否触发 ReplicatedUsing 。如果Client本地修改了这个属性,导致网络包到达时发现属性相同,那么就不会触发 ReplicatedUsing 的函数。
OnRep_ 函数可以用一个参数来装旧值。
存档系统
Cast to 蓝图节点可以右键转换为 pure cast,此时不再有执行引脚。使用 pure cast 来预防出现静默失效的情况(cast 失败会报错,类似断言)。
World Partition 是 UE5 才引入的新一代大世界流送系统,在 UE4 中对应的替代方案是 World Composition(世界场景构成)。使用 WorldPartition 后,在 GameMode::InitGame 时,场景中Actor不全。
UPROPERTY(SaveGame)
UMG菜单
UE要求必须有Level对应,所以即使MainMenu UserWidget不需要,也需要创建个对应的Level。
避免纹理拉伸。Draw as :Box、Border、Image
动画蓝图
如果蓝图变量与原生父类的变量同名且类型相同,引擎会自动移除蓝图变量,并将所有引用重定向到C++原生变量上,整个过程只会记录一条日志信息。
EventGraph处理变量;AnimGraph则用这些变量去混合动画
数据资产
Data Tables:策划填表来。
Data Assets:继承 PrimaryDataAsset 创建,数据打包。
硬引用:默认。就算只是用到了 Cast<T>,也会把T加载。
软引用:异步,用起来比较复杂,但是能提升性能。手动控制加载时机。
SIZE MAP:抓取所有 hard references 显示所有被加载的尺寸。右键资产打开
REFERENCE VIEWER:View Asset References, often reveal unnecessary Cast<T> or references to other content.
TSoftObjectPtr<T> 代替一般的指针
Async Loading 异步加载:
加载单个:FStreamableManager (C++) or Blueprint nodes such as “Async Load Asset”.
TSoftClassPtr<T> —— TSubclassOf<T>;TSoftObjectPtr<T> ——一般指针
Asset Manager,加载多个,使用Id,需要编辑器配置。
必要的数据记得在rule里设置Always Cook。
UAssetManager* Manager = UAssetManager::GetIfInitialized(); 而不是GetIfValid();
| 改动类型 | 需要做什么 |
|---|---|
| 改 C++ 代码(重写函数、改逻辑) | 编译 + 重启编辑器 |
改 DefaultGame.ini | 重启编辑器 |
改 GetPrimaryAssetId() 的返回类型名 | 编译 + 重新保存所有相关资产 |
| 改资产类(换父类、加属性) | 编译 + 重新保存资产 |
性能优化
简单方法
避免Tick调用;用Events或者Timers降低频率
碰撞设置:非必要不GenerateOverlapEvents;CollisionProfiles只对特定channels反应或者禁用小物体/无法接触的物体。尽量“QueryOnly”,除非需要物理模拟(力、重力)。
避免硬引用,这会影响加载时间和蓝图编译时间。
stat 命令(运行时输入
显示标记部分代码的CPU消耗。
DECLARE_STATS_GROUP 建组,DECLARE_CYCLE_STAT 填项,SCOPE_CYCLE_COUNTER 计时,stat 组显示名 查看。
Unreal Insights
对package的游戏使用。
UE4 Performance and Profiling
https://www.youtube.com/watch?v=hcxety8gfs
Overcoming Common Early Challenges in Unreal Engine
https://www.youtube.com/watch?v=2edoacF53F0
Blueprints In-depth (incl. Project Architecture and Relationship to C++)
https://www.youtube.com/watch?v=j6mskTgL7kU
Collect, Analyze, and Visualize Your Data with Unreal Insights
https://www.youtube.com/watch?v=Rf6oNkcGmX4
修bug:
编辑器设置:Promote Output Log Warnings During PIE 启用;Blueprint Break on Exceptions启用
SAMPLE PROJECTS
Content Examples
// HUGE range of engine features covered, probably the most interesting project with the least exciting name
// Animation, Blueprints, VFX, Audio, Multiplayer, and the list goes o…
ActionRPG
// Gameplay Ability System based C++ project.
ShooterGame
// Gets occasional new engine features such as new spatial audio and ‘replication graph’.
SurvivalGame
https://github.com/tomlooman/EpicSurvivalGameSeries
ActionRoguelike
// Will continue to be updated with features and code samples.
https://github.com/tomlooman/ActionRoguelike
Subsystems, 编辑器拓展
- 标题: UE C++·TomLooman的教程学习笔记
- 作者: 铁名_IronName
- 创建于 : 2026-08-26 10:23:11
- 更新于 : 2026-09-30 16:19:44
- 链接: https://blog.ironname.top/2026/UE/UE学习笔记2/
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。