【发布时间】:2019-03-13 17:50:00
【问题描述】:
我正在尝试使用 for 循环创建多个 UStaticMeshComponent,但虚幻引擎不断在 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;
};
C++
#include "FlowSphere.h"
// Sets default values
AFlowSphere::AFlowSphere()
{
int32 amount = 100;
RootComponent = CreateDefaultSubobject<USceneComponent>("SceneComponent");
for (int32 i = 0; i < amount; i++) {
//WHEN I INCLUDE THE LINE BELOW, UE4 START MAKING BREAKPOINT
UStaticMeshComponent* item = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Sphere"));
item.AttachTo(this->RootComponent);
}
//THIS WORKS FINE
this->test = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("HELLO"));
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AFlowSphere::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFlowSphere::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
它在 UE4 API 的 Object.h 上触发断点
/**
* Create a component or subobject
* @param TReturnType class of return type, all overrides must be of this type
* @param SubobjectName name of the new component
* @param bTransient true if the component is being assigned to a transient property. This does not make the component itself transient, but does stop it from inheriting parent defaults
*/
template<class TReturnType>
TReturnType* CreateDefaultSubobject(FName SubobjectName, bool bTransient = false)
{
UClass* ReturnType = TReturnType::StaticClass();
return static_cast<TReturnType*>(CreateDefaultSubobject(SubobjectName, ReturnType, ReturnType, /*bIsRequired =*/ true, /*bIsAbstract =*/ false, bTransient));
}
【问题讨论】:
标签: c++ unreal-engine4