gpt4 book ai didi

c++ - 虚幻引擎 : How can I create UStaticMeshComponent in a loop?

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:05 24 4
gpt4 key购买 nike

我正在尝试使用 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));
}

最佳答案

您对 CreateDefaultSubobject 的两次不同调用使用了相同的名称。

我在新的 UE4 C++ 项目中运行您的代码并收到以下错误消息:

Fatal error: [File:D:\Build++UE4\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 3755] Default subobject StaticMeshComponent Sphere already exists for FlowSphere /Script/MyProject.Default__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;

}

关于c++ - 虚幻引擎 : How can I create UStaticMeshComponent in a loop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55148309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com