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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: b'ue4 actor component'是Unreal Engine 4的一个组件,可以附加到Actor上,并添加自定义功能,例如碰撞检测、动画控制和游戏逻辑。这样,多个Actor可以共享同一个组件,从而简化代码和提高效率。 ### 回答2: UE4 Actor Component是一个非常重要的功能模块,它可以让我们为UE4引擎的游戏角色添加自定义的行为和功能,而不需要修改原有的游戏代码。使用Actor Component,我们可以将游戏角色所拥有的行为和功能拆分成一个个独立的模块,并可以独立开发、测试、优化和修改,从而提高游戏的可维护性和开发效率。以下是Actor Component的主要特性: 1. 重用性:可以将Actor Component应用到不同的游戏角色,从而将相同的行为和功能重用。 2. 灵活性:可以根据需要添加或移除Actor Component,从而增强游戏角色的行为和功能。 3. 可拆分性:可以将复杂的游戏角色拆分成更小、更简单的组件,从而降低代码复杂度和维护成本。 4. 可扩展性:通过继承Actor Component类,我们可以创建自己的定制组件,从而增强游戏的定制化程度。 使用Actor Component开发游戏可以大大简化代码开发过程,加快开发时间,减少错误和bug,提高代码的可读性和可维护性。Actor ComponentUE4引擎的一个重要特性,也是开发高质量游戏的不可或缺的工具之一。 ### 回答3: UE4(Unreal Engine 4)是一款功能强大的游戏开发引擎,其的ActorComponent模块是构建游戏对象的重要组成部分之一。Actor是在游戏场景的一个物体,而Component是Actor对象的一部分,经常用于添加特定功能和属性。UE4的Actor Component能够帮助游戏开发人员在游戏实现更多的交互和动态效果。 UE4 Actor Component的设计思路是将不同的组件将Actor划分为不同的模块,使其更加简单易用和灵活。通过UE4 Actor Component,开发人员可以根据自己的需求将多个组件组合在一个Actor,从而实现更复杂的游戏特效等功能。 UE4 Actor Component提供了很多常见的组件,如动画组件、碰撞器组件、模型组件、声音组件等,以及多种自定义组件。其,动画组件是游戏开发最常用的组件之一,可以使游戏的角色动作更加真实,使动画更加流畅。碰撞器组件则可以增加游戏的碰撞检测效果,模型组件可以快速地在场景添加物体,声音组件则可以为游戏增加更多音效。 UE4 Actor Component的另一个优势是可以通过连接多个组件来创建更为复杂的组件。通过将一个组件附加到另一个组件上,可以将两个组件的功能融合在一起,从而实现更多不同的游戏特效。例如,一个具有紫外线视觉效果的球体可以通过将光线源附加到它的子组件上来实现,这样就可以使球体在场景发出紫外线光线。 总之,UE4的Actor Component是游戏开发非常重要的一部分,它能够提供丰富多彩的特效和功能,游戏开发人员可以通过组合不同的组件来实现自己的游戏开发需求。因此,学习UE4 Actor Component对于游戏开发人员来说是非常有必要的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值