C++ 虚幻引擎:如何在循环中创建组件?
我试图使用for循环创建多个C++ 虚幻引擎:如何在循环中创建组件?,c++,unreal-engine4,C++,Unreal Engine4,我试图使用for循环创建多个UStaticMeshComponent,但Unreal引擎一直在Object.h中的CreateDefaultSubobject上触发断点(不在我的代码中,它来自UE4核心API)。当我创建单个组件时,它工作正常。我对UnrealEngine和C++很陌生,所以我可能会做一些傻事,但是请你不要太在意: 非常感谢你的帮助 标题 #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h"
UStaticMeshComponent
,但Unreal引擎一直在Object.h
中的CreateDefaultSubobject
上触发断点(不在我的代码中,它来自UE4核心API)。当我创建单个组件时,它工作正常。我对UnrealEngine和C++很陌生,所以我可能会做一些傻事,但是请你不要太在意:
非常感谢你的帮助
标题
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FlowSphere.generated.h"
UCLASS()
class CPP_PRACTICE_3_API AFlowSphere : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFlowSphere();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
//UPROPERTY()
//TArray<UStaticMeshComponent*> StaticMeshComponents;
UStaticMeshComponent* test;
};
您对CreateDefaultSubobject的两个不同调用使用了相同的名称 < >我在新的UE4C++项目中运行了代码,并获得以下错误信息: 致命错误: [文件:D:\Build++UE4\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [行:3755]默认子对象StaticMeshComponent球体已存在 FlowSphere/Script/MyProject.Default\uuuu FlowSphere存在 另外,您发布的代码没有编译
item.AttachTo(this->RootComponent);
应该是:
item->AttachTo(this->RootComponent);
您还需要包括“Components/StaticMeshComponent.h”。以下是更正后的代码:
#include "FlowSphere.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
AFlowSphere::AFlowSphere()
{
int32 amount = 100;
RootComponent = CreateDefaultSubobject<USceneComponent>("SceneComponent");
for (int32 i = 0; i < amount; i++) {
FName name = *FString::Printf(TEXT("Sphere %i"), i);
UStaticMeshComponent* item = CreateDefaultSubobject<UStaticMeshComponent>(name);
item->AttachTo(this->RootComponent);
}
this->test = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("HELLO"));
PrimaryActorTick.bCanEverTick = true;
}
#包括“FlowSphere.h”
#包括“组件/StaticMeshComponent.h”
//设置默认值
AFlowSphere::AFlowSphere()
{
int32金额=100;
RootComponent=CreateDefaultSubobject(“SceneComponent”);
对于(int32 i=0;i附件(此->根组件);
}
此->测试=CreateDefaultSubobject(文本(“HELLO”);
PrimaryActorTick.bCanEverTick=true;
}
所以我最近不得不做同样的事情,遇到了很多问题,但需要解决的两个主要问题是:
#include "FlowSphere.h"
#include "Components/StaticMeshComponent.h"
#include "UObject/ConstructorHelpers.h"
// Sets default values
AFlowSphere::AFlowSphere()
{
int32 amount = 100;
RootComponent = CreateDefaultSubobject<USceneComponent>("SceneComponent");
for (int32 i = 0; i < amount; i++) {
FName name = *FString::Printf(TEXT("Sphere %i"), i);
FName c_name = *FString::Printf(TEXT("ChildSceneComponent %i"), i);
UPROPERTY(EditAnywhere)
USceneComponent* ChildSceneComponent
= CreateDefaultSubobject<USceneComponent>(c_name);
// instead of the regular static mesh, create and instance static mesh object with name assigned
UInstancedStaticMeshComponent* item = CreateDefaultSubobject<UStaticMeshComponent>(name);
item->RegisterComponent();
// assign property to mesh if not you'll get a static mesh null property error like i did
auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
if (MeshAsset.Object != nullptr)
{
item->SetStaticMesh(MeshAsset.Object);
}
item->SetFlags(RF_Transactional);
// add instance to actor level
this->AddInstanceComponent(item);
// make the scene component for the mesh to be visible
RootComponent = Root;
// attach mesh to the scene component of the actor in form of a Hierarchy
item->AttachToComponent(ChildSceneComponent, FAttachmentTransformRules::KeepWorldTransform, m_name);
ChildSceneComponent->AttachToComponent(Root, FAttachmentTransformRules::KeepWorldTransform, c_name);
// set mesh transform based on the ith position
FTransform t(FVector(250 * i, i, i));
// activate the new copy of the mesh
Mesh->AddInstance(t);
// Offset and scale the child scene from the root scene as a precaution
ChildSceneComponent->SetRelativeTransform(
FTransform(FRotator(0, 0, 0),
FVector(250 * i, i, i),
FVector(0.1f))
);
}
PrimaryActorTick.bCanEverTick = true;
}
#包括“FlowSphere.h”
#包括“组件/StaticMeshComponent.h”
#包括“UObject/ConstructorHelpers.h”
//设置默认值
AFlowSphere::AFlowSphere()
{
int32金额=100;
RootComponent=CreateDefaultSubobject(“SceneComponent”);
对于(int32 i=0;iRegisterComponent();
//将属性指定给网格,否则会像我一样出现静态网格null属性错误
auto-MeshAsset=ConstructorHelpers::FObjectFinder(文本(“StaticMesh'/Engine/BasicShapes/Sphere.Sphere');
if(MeshAsset.Object!=nullptr)
{
item->SetStaticMesh(MeshAsset.Object);
}
项目->设置标志(RF_事务);
//将实例添加到参与者级别
此->添加组件(项目);
//使网格的场景组件可见
RootComponent=根;
//将网格以层次的形式附加到角色的场景组件
item->AttachToComponent(ChildSceneComponent,FAttachmentTransformRules::KeepWorldTransform,m_名称);
ChildSceneComponent->AttachToComponent(根,FAttachmentTransformRules::KeepWorldTransform,c_名称);
//基于第i个位置设置网格变换
f变换t(FVector(250*i,i,i));
//激活网格的新副本
网格->附加值(t);
//作为预防措施,从根场景偏移并缩放子场景
ChildSceneComponent->SetRelativeTransform(
F变换(FRotator(0,0,0),
FVector(250*i,i,i),
FVector(0.1f))
);
}
PrimaryActorTick.bCanEverTick=true;
}
#include "FlowSphere.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
AFlowSphere::AFlowSphere()
{
int32 amount = 100;
RootComponent = CreateDefaultSubobject<USceneComponent>("SceneComponent");
for (int32 i = 0; i < amount; i++) {
FName name = *FString::Printf(TEXT("Sphere %i"), i);
UStaticMeshComponent* item = CreateDefaultSubobject<UStaticMeshComponent>(name);
item->AttachTo(this->RootComponent);
}
this->test = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("HELLO"));
PrimaryActorTick.bCanEverTick = true;
}
#include "FlowSphere.h"
#include "Components/StaticMeshComponent.h"
#include "UObject/ConstructorHelpers.h"
// Sets default values
AFlowSphere::AFlowSphere()
{
int32 amount = 100;
RootComponent = CreateDefaultSubobject<USceneComponent>("SceneComponent");
for (int32 i = 0; i < amount; i++) {
FName name = *FString::Printf(TEXT("Sphere %i"), i);
FName c_name = *FString::Printf(TEXT("ChildSceneComponent %i"), i);
UPROPERTY(EditAnywhere)
USceneComponent* ChildSceneComponent
= CreateDefaultSubobject<USceneComponent>(c_name);
// instead of the regular static mesh, create and instance static mesh object with name assigned
UInstancedStaticMeshComponent* item = CreateDefaultSubobject<UStaticMeshComponent>(name);
item->RegisterComponent();
// assign property to mesh if not you'll get a static mesh null property error like i did
auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
if (MeshAsset.Object != nullptr)
{
item->SetStaticMesh(MeshAsset.Object);
}
item->SetFlags(RF_Transactional);
// add instance to actor level
this->AddInstanceComponent(item);
// make the scene component for the mesh to be visible
RootComponent = Root;
// attach mesh to the scene component of the actor in form of a Hierarchy
item->AttachToComponent(ChildSceneComponent, FAttachmentTransformRules::KeepWorldTransform, m_name);
ChildSceneComponent->AttachToComponent(Root, FAttachmentTransformRules::KeepWorldTransform, c_name);
// set mesh transform based on the ith position
FTransform t(FVector(250 * i, i, i));
// activate the new copy of the mesh
Mesh->AddInstance(t);
// Offset and scale the child scene from the root scene as a precaution
ChildSceneComponent->SetRelativeTransform(
FTransform(FRotator(0, 0, 0),
FVector(250 * i, i, i),
FVector(0.1f))
);
}
PrimaryActorTick.bCanEverTick = true;
}