UE4 Actor之Actor流程分析(一) 您所在的位置:网站首页 深圳瑞橙网络科技有限公司怎么样 UE4 Actor之Actor流程分析(一)

UE4 Actor之Actor流程分析(一)

#UE4 Actor之Actor流程分析(一)| 来源: 网络整理| 查看: 265

前言文章之后会修改.最近一些文章互相关联性较高, 可能随着某篇文章某些内容的深扒, 而导致一些内容更连贯, 并对所有相关内容进行修改源码版本4.22大钊:《InsideUE4》GamePlay架构(一)Actor和Component大钊:《InsideUE4》GamePlay架构(二)Level和WorldUE创世,万物皆UObject,接着有Actor。

这是一篇Actor生成到销毁完成流程的代码分析系列, 总篇幅较长.

因为各种原因, 会拆分成多篇文章, 每篇文章相对会独立, 但互相关联性很大.

由于Actor类的生成销毁在UObject基础上, UObject啃不动, 之后单做分析.

Actor Lifecycle Unreal Engine 4 - Game Flow and Actor Lifecycle Overview - bright developers

嗯, 这个看不懂? 看下面的 (出处AActor头文件):

/** * Actor is the base class for an Object that can be placed or spawned in a level. * Actors may contain a collection of ActorComponents, which can be used to control how actors move, how they are rendered, etc. * The other main function of an Actor is the replication of properties and function calls across the network during play. * * Actor initialization has multiple steps, here's the order of important virtual functions that get called: * - UObject::PostLoad: For actors statically placed in a level, the normal UObject PostLoad gets called both in the editor and during gameplay. * This is not called for newly spawned actors. * - UActorComponent::OnComponentCreated: When an actor is spawned in the editor or during gameplay, this gets called for any native components. * For blueprint-created components, this gets called during construction for that component. * This is not called for components loaded from a level. * - AActor::PreRegisterAllComponents: For statically placed actors and spawned actors that have native root components, this gets called now. * For blueprint actors without a native root component, these registration functions get called later during construction. * - UActorComponent::RegisterComponent: All components are registered in editor and at runtime, this creates their physical/visual representation. * These calls may be distributed over multiple frames, but are always after PreRegisterAllComponents. * This may also get called later on after an UnregisterComponent call removes it from the world. * - AActor::PostRegisterAllComponents: Called for all actors both in the editor and in gameplay, this is the last function that is called in all cases. * - AActor::PostActorCreated: When an actor is created in the editor or during gameplay, this gets called right before construction. * This is not called for components loaded from a level. * - AActor::UserConstructionScript: Called for blueprints that implement a construction script. * - AActor::OnConstruction: Called at the end of ExecuteConstruction, which calls the blueprint construction script. * This is called after all blueprint-created components are fully created and registered. * This is only called during gameplay for spawned actors, and may get rerun in the editor when changing blueprints. * - AActor::PreInitializeComponents: Called before InitializeComponent is called on the actor's components. * This is only called during gameplay and in certain editor preview windows. * - UActorComponent::Activate: This will be called only if the component has bAutoActivate set. * It will also got called later on if a component is manually activated. * - UActorComponent::InitializeComponent: This will be called only if the component has bWantsInitializeComponentSet. * This only happens once per gameplay session. * - AActor::PostInitializeComponents: Called after the actor's components have been initialized, only during gameplay and some editor previews. * - AActor::BeginPlay: Called when the level starts ticking, only during actual gameplay. * This normally happens right after PostInitializeComponents but can be delayed for networked or child actors. * * @see https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/ * @see https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Actors/ActorLifecycle * @see UActorComponent */

啥? 看不懂, 嗯, 我也看不懂 (笑)

这里先是对右方流程的分析, 也是最常用的的流程 :

UWorld::SpawnActor

所有生成Actor最终跳转到这个函数, 参数有三个:

Class类UserTransformPtr位置FActorSpawnParameters信息

先看其中这一段代码 :

一个UWorld可以有多个ULevel, 每个ULevel里面有各自的AActor.

这里的操作就是获取AActor最后归属的ULevel.

分析可得, 该值的取值顺序依次是:

SpawnParameters.OverrideLevelCastChecked(SpawnParameters.Owner->GetOuter())如果是编辑器模式, 在关卡编辑器中选中的当前关卡CurrentLevel否则, 是主关卡PersistentLevel

且该值类始终为ULevel

接着看, 这里有一个返回情况

类是否存在, 不存在则日志警告并返回

再往下, 这里有一个AActor时间记录的结构体, 通过作用域方式实现, 并贴上下文中的唯一的一个调用

// 如果看不懂这种写法, 参考下面这个网址 c++作用域_百度搜索

330-367行, 一堆判定Actor是否可以生成的if-else, 详情见注释.

中间条件(bIsRunningConstructionScript && !SpawnParameters.bAllowDuringConstructionScript)不清楚是什么情况.

谷歌没有找到相关信息, 如有了解, 希望能指教一下.

接着往下, 这里分情况, 设置了FName NewActorName和Template两个属性.

这里有个check, 得出Template必定存在, 不存在取Actor的默认对象

而NewActorName在Template存在的时候, 取唯一值.

UObject相关, 忽略掉

往下看, 这里有一道检查, 途中的函数为虚函数, 可以重写, 用于控制AActor能否在各端生成.

接着往下, 这里是对Actor生成位置的赋值

// 可以看到, 这里的目的是取一个生成位置 // 而上文的位置检测是检测位置是否无效, 无效位置 // Returns true if the vector contains a component that is either NAN or +/-infinite. // 参考Vector, 嗯, 不明觉厉

接着往下, 是对CollisionHandlingOverride的重新赋值, 并检测

由于这个CollisionHandlingOverride值的存在, 此时AActor的位置信息, 位置并不一定是最终生成的位置, 往下看就知道了

/** Method for resolving collisions at the spawn point. Undefined means no override, use the actor's setting. */

用于当Actor生成之后的位置会发生碰撞, 这个时候该如何处理. 这里根据SpawnParameters.bNoFail值进行修改, 并且检测并处理有碰撞不生成的情况

接下来, 到了最核心的代码 : NewObject.

参数由前文得出, 具体细节略过(笑).

忽略

AActor对应的ULevel中添加AActor, 进行关联

AActor::PostSpawnInitialize下篇文章分析

生成失败, 暂时没遇到过, 不清楚, 为什么会有这种情况.

两个多播和最后一个添加网络Actor的函数.

嗯, 流程上到这里就算完成了, 一个Actor就生成了.

孤傲雕:UE4 Actor之Actor流程分析(二)

结语补更一篇(水完), 仍欠一篇由于AActor, 包括前段时间的AGameModeBase, 这些类属于核心类, 和其他类或者功能各种关联耦合,能力有限, 许多地方读不懂, 等其他地方代码读完之后, 再做修复.慢慢的, 所有东西就都关联起来了, 也就成体系有内容了, 水分也就没那么多了(笑)嗯, 骗收藏吧.


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有