UE4中dynamic create component及ChildActorComponent未解析符号 问题

本文介绍在Unreal Engine中如何动态创建组件,包括使用CreateDefaultSubobject在构造函数中初始化组件的方法及其限制,以及如何在运行时创建并注册组件的具体步骤。此外,还详细解释了ChildActorComponent组件的正确使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.dynamic create component

正常方法添加组件方法是

On Character.h I add a USpringArm and UChildActor component :

             UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = InventoryCamera)
                 TSubobjectPtr<class USpringArmComponent> InventorySpringArm;
         
             UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = InventoryCamera)
                 TSubobjectPtr<class UChildActorComponent> InventoryCamera;
     

On Character.cpp Constructor I add this code to Initialize those Actors :

             InventorySpringArm = PCIP.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("InventorySpringArm"));
             InventorySpringArm->AttachTo(CapsuleComponent);
             InventorySpringArm->bUseControllerViewRotation = false;
         
             InventoryCamera = PCIP.CreateDefaultSubobject<UChildActorComponent>(this, TEXT("InventoryCamera"));
             InventoryCamera->ChildActorClass = UCameraComponent::StaticClass();
             InventoryCamera->CreateChildActor(); // <-- this code throws a Compile Error下面会说道childactor这么生成会有问题的
             InventoryCamera->AttachTo(InventorySpringArm);
ps:也有的版本PCIP写成ObjectInitiallizer

但这个方法问题在于CreateDefaultSubobject 只能在构造函数里面调用

因此想要动态生成组件方法是:

Creating and Registering Components During Runtime

Code:
//in some AActor class

void AYourActor::CreateComponent(UClass* CompClass,const FVector& Location, const FRotator& Rotation, const FName& AttachSocket=NAME_None)
{
  FName YourObjectName("Hiiii");

  //CompClass can be a BP
  UPrimitiveComponent* NewComp = ConstructObject<UPrimitiveComponent>( CompClass, this, YourObjectName);
  if(!NewComp) 
 {
    return NULL;
 }
 //~~~~~~~~~~~~~

  NewComp->RegisterComponent();        //You must ConstructObject with a valid Outer that has world, see above	 
  NewComp->SetWorldLocation(Location); 
  NewComp->SetWorldRotation(Rotation); 
  NewComp->AttachTo(GetRootComponent(),SocketName,EAttachLocation::KeepWorldPosition); 
   //could use different than Root Comp

参考:

1.https://forums.unrealengine.com/showthread.php?52410-What-is-the-correct-way-to-create-and-add-components-at-runtime


2.ChildActorComponent的createChildActor方法使用,未解析外部引用

查看ChildActorComponent.h的源代码就知道

中间有

//start interface

......

//end interface

的注释,createChildActor方法在下面,并没有暴露出来

这里要使用childActorComponent->OnComponentCreated();方法

它会内部调用createChildActor方法

参考:

1.https://answers.unrealengine.com/questions/90283/unresolved-externals-error-with-uchildactorcompone.html

2.https://answers.unrealengine.com/questions/101002/uchildcomponentactor-createchildactor-compile-erro.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值