GamePlay架构

本文部分引用下方资料中的内容

资料:
InsideUE4

官方文档

编译游戏项目

代码规范

基本概念

在UE4中,几乎所有的对象都继承于UObject。

在UE中,3D世界是由Actors构建起来的,而Actor又拥有各种Component,之后又有各种Controller可以控制Actor(Pawn)的行为。

在UE4中,你也可以为一个Actor添加一个蓝图或者C++ Component,然后实现它来直接组织逻辑。

编译系统

UnrealBuildTool(UBT,C#):UE4的自定义工具,来编译UE4的逐个模块并处理依赖等。编写的Target.cs,Build.cs都是为这个工具服务的。

UnrealHeaderTool (UHT,C++):UE4的C++代码解析生成工具,在代码里写的那些宏UCLASS等和#include "*.generated.h"都为UHT提供了信息来生成相应的C++反射代码。

Actor

小到一个个地上的石头,大到整个世界的运行规则,都是Actor

Transform封装进了SceneComponent,当作RootComponent。便利性的Actor方法,如(Get/Set)ActorLocation等,其实内部都是转发到RootComponent。

/*~
 * Returns location of the RootComponent 
 * this is a template for no other reason than to delay compilation until USceneComponent is defined
 */ 
template<class T>
static FORCEINLINE FVector GetActorLocation(const T* RootComponent)
{
    return (RootComponent != nullptr) ? RootComponent->GetComponentLocation() : FVector(0.f,0.f,0.f);
}
bool AActor::SetActorLocation(const FVector& NewLocation, bool bSweep, FHitResult* OutSweepHitResult, ETeleportType Teleport)
{
    if (RootComponent)
    {
        const FVector Delta = NewLocation - GetActorLocation();
        return RootComponent->MoveComponent(Delta, GetActorQuat(), bSweep, OutSweepHitResult, MOVECOMP_NoFlags, Teleport);
    }
    else if (OutSweepHitResult)
    {
        *OutSweepHitResult = FHitResult();
    }
    return false;
}

Component

Actor们轻装上阵,只提供一些通用的基本生存能力,而把众多的“技能”抽象成了一个个“Component”并提供组装的接口,让Actor随用随组装,把自己武装成一个个专业能手。

TSet OwnedComponents 保存着这个Actor所拥有的所有Component,一般其中会有一个SceneComponent作为RootComponent。

TArray InstanceComponents 保存着实例化的Components。

一个Actor若想可以被放进Level里,就必须实例化USceneComponent* RootComponent。

ActorComponent下面最重要的一个Component就非SceneComponent莫属了。

SceneComponent提供了两大能力:一是Transform,二是SceneComponent的互相嵌套。

GamePlay架构_第1张图片
组合优于继承

Actor其实更像是一个容器,只提供了基本的创建销毁,网络复制,事件触发等一些逻辑性的功能,而把父子的关系维护都交给了具体的Component

Level

你可能感兴趣的:(GamePlay架构)