- ue这里面,蓝图和cpp是两套运行顺序逻辑。首先是构造函数,cpp的构造函数是优于一切的,而蓝图的伪构造,其实是创建实例。BluePrintClass就是类似Unity里面的Prefab,拖到场景中才会创建实例。
- 组件子Actor的构建,可以在构造函数中CreateDefaultSubobject <ChildActorComponent>进行;但是这个组件只是一个胶水,在蓝图里静态存在时,并不是真正的创建Actor(见下面大钊文章)。运行时子Actor的构建,可以GetWorld.SpawnActor进行。编辑器中如果想动态构建,最好全走蓝图,有一些蓝图节点蓝图能调用cpp中没有;也可以尝试写一些BluePrintLibrary。
《InsideUE4》GamePlay架构(一)Actor和Component - 知乎 (zhihu.com)
UE4中dynamic create component及ChildActorComponent未解析符号 问题_繁弱的博客-CSDN博客
UE4 中在 Actor 中动态 Create Component 与ChildActor 的 小笔记 (bbsmax.com)
《InsideUE4》GamePlay架构(一)Actor和Component - 知乎 (zhihu.com)
动态创建物体;
CreateDefaultSubobject只能在构造函数中使用
c++ - Unreal Engine 4. Different ways to instantiate the object - Stack Overflow
废代码记录
UChildActorComponent* ChildActorComponent = CreateDefaultSubobject<UChildActorComponent>("Slot");
ChildActorComponent->SetChildActorClass(ASlot::StaticClass());
// ChildActorComponent->SetupAttachment(RootComponent);
ChildActorComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
auto subObj = CreateDefaultSubobject<ASlot>("Slot");//CreateDefaultSubobject是只能在cpp的构造函数中使用吗?
auto subRoot = subObj->GetRootComponent();
subRoot->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
subObj->AttachToActor(this, FAttachmentTransformRules::KeepRelativeTransform);
childSlots.Emplace(subObj);
//UE_LOG(TDLog, Error, TEXT("行列:%d, %d"), numberOfRows, numberOfRows);
for (int i = 0; i < numberOfRows - 1; ++i)
{
for (int j = 0; j < numberOfColumns - 1; ++j)
{
// UChildActorComponent* ChildActorComponent =
// Cast< UChildActorComponent >
// (
// FDatasmithActorImporter::ImportSceneComponent(
// UChildActorComponent::StaticClass(),
// AreaLightElement, ImportContext, Outer, UniqueNameProvider ) );
//
// ChildActorComponent->SetChildActorClass( TSubclassOf< AActor >
// ( LightShapeBlueprint->GeneratedClass ) );
// UChildActorComponent* ChildActorComponent = CreateDefaultSubobject<UChildActorComponent>("Slot");
// ChildActorComponent->SetChildActorClass(TSubclassOf<ASlot>());
// ChildActorComponent->SetupAttachment(RootComponent);
// const auto temp_childActorComponent = NewObject<UChildActorComponent>(this, UChildActorComponent::StaticClass());
// UObject* outer = nullptr;
// auto temp_imageCom = NewObject<ASlot>(outer);
// temp_imageCom.registercom
// temp_childActorComponent->SetChildActorClass(TSubclassOf<ASlot>());
// temp_imageCom->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform);//SnapToTargetNotIncludingScale
auto subObj = CreateDefaultSubobject<ASlot>("Slot");
subObj->AttachToActor(this, FAttachmentTransformRules::KeepRelativeTransform);
childSlots.Emplace(subObj);
}
}