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函数来实现附加操作。