UE5虚幻引擎 Actor源码解析

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;

解析:这段程序是关于获取和设置Actor位置的代码。

GetActorLocation模板函数,用于获取Actor的位置。它接受一个指向RootComponent的指针作为参数,并返回RootComponent的位置。如果RootComponent不为nullptr,则返回其位置;否则返回一个坐标值为(0, 0, 0)的FVector。

SetActorLocation是AActor类的成员函数,用于设置Actor的位置。它接受一个新的位置NewLocation、一个布尔值bSweep、一个指向FHitResult的指针OutSweepHitResult以及一个ETeleportType类型的参数Teleport。在函数内部,首先检查RootComponent是否存在。如果存在,计算位置的偏移量Delta,并调用RootComponent的MoveComponent函数来移动Actor的位置。MoveComponent函数接受位置偏移量、Actor的旋转值、是否进行Sweep检测、一个指向FHitResult的指针以及一些标志位作为参数。如果RootComponent不存在,则将OutSweepHitResult置空。最后,返回一个布尔值表示设置位置是否成功。

简而言之,这段代码实现了获取和设置Actor位置的功能。GetActorLocation用于获取Actor的位置,而SetActorLocation用于设置Actor的位置并进行相应的移动操作。

void AActor::AttachToActor(AActor* ParentActor, const FAttachmentTransformRules& AttachmentRules, FName SocketName)
{
    if (RootComponent && ParentActor)
    {
        USceneComponent* ParentDefaultAttachComponent = ParentActor->GetDefaultAttachComponent();
        if (ParentDefaultAttachComponent)
        {
            RootComponent->AttachToComponent(ParentDefaultAttachComponent, AttachmentRules, SocketName);
        }
    }
}
void AActor::AttachToComponent(USceneComponent* Parent, const FAttachmentTransformRules& AttachmentRules, FName SocketName)
{
    if (RootComponent && Parent)
    {
        RootComponent->AttachToComponent(Parent, AttachmentRules, SocketName);
    }
}

解析:这段程序是关于将Actor附加到其他Actor或场景组件的代码。

AttachToActor是AActor类的成员函数,用于将当前Actor附加到另一个父级Actor上。它接受一个指向父级Actor的指针ParentActor、一个FAttachmentTransformRules类型的引用AttachmentRules和一个SocketName作为参数。在函数内部,首先检查当前Actor的RootComponent和父级Actor是否存在。如果它们都存在,获取父级Actor的默认附加组件ParentDefaultAttachComponent。如果ParentDefaultAttachComponent存在,那么就调用当前Actor的RootComponent的AttachToComponent函数,将其附加到ParentDefaultAttachComponent上,使用给定的AttachmentRules和SocketName。

AttachToComponent也是AActor类的成员函数,用于将当前Actor附加到一个场景组件上。它接受一个指向场景组件的指针Parent、一个FAttachmentTransformRules类型的引用AttachmentRules和一个SocketName作为参数。在函数内部,首先检查当前Actor的RootComponent和Parent是否存在。如果它们都存在,就调用当前Actor的RootComponent的AttachToComponent函数,将其附加到Parent上,使用给定的AttachmentRules和SocketName。

简而言之,这段代码实现了将当前Actor附加到其他Actor或场景组件上的功能。AttachToActor函数将当前Actor附加到另一个父级Actor上,而AttachToComponent函数将当前Actor附加到一个场景组件上。这些函数通过调用RootComponent的AttachToComponent函数来实现附加操作。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值