0

this is the fix at top use this to create a string that Static Mesh Object can accept as a unique identifier so the engine doesn't crash

FName name = *FString::Printf(TEXT("Sphere %i"), i);

I want to use a for Loop to create static meshes that I can reference later and link to other events over OSC. I am having a problem figuring out how to name the instances using the CreateDefaultSubobject.

I get this error when Compiling my code:

S:\UnrealEngine\CPP_LEARNING\ShaderShootEm_000\Source\ShaderShootEm_000\FlowSphere.cpp(19): error C4456: declaration of 'i' hides previous local declaration 
S:\UnrealEngine\CPP_LEARNING\ShaderShootEm_000\Source\ShaderShootEm_000\FlowSphere.cpp(18): note: see declaration of 'i' 
S:\UnrealEngine\CPP_LEARNING\ShaderShootEm_000\Source\ShaderShootEm_000\FlowSphere.cpp(23): error C4800: Implicit conversion from 'int32' to bool. Possible information loss 
S:\UnrealEngine\CPP_LEARNING\ShaderShootEm_000\Source\ShaderShootEm_000\FlowSphere.cpp(23): note: consider using explicit cast or comparison to 0 to avoid this warning 
C:\Program Files\Epic Games\UE_5.0\Engine\Source\Runtime\Core\Public\HAL\Platform.h(998): note: see declaration of 'int32'

This is the Code

RootComponent = CreateDefaultSubobject<USceneComponent>("SceneComponent");
FVector CurrentLocation = GetActorLocation();
StaticMeshes.Init(NULL,10);
float distance = 0;
int32 i = 0;
 for(int32 i= 0; i < 5; i++)
 {
    FString Number = FString::FromInt(i);
    distance *= i * 100;
    StaticMeshes[i] = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Number %i"),i );
    StaticMeshes[i]->SetRelativeLocation(CurrentLocation + FVector(0.0f, 0.0f, distance));
    StaticMeshes[i]->SetupAttachment(RootComponent);
 }

thanks for any help in advance~

GSchian0
  • 1
  • 2
  • 1) Your `int32 i` index of the loop is hiding the `int32 i` defined just before the loop. 2) It seems like `CreateDefaultSubobject` expects a `bool` param and you pass it `i` which is an `int32`. – wohlstad May 13 '22 at 16:21
  • I'm not sure how to change the name of each instance using the TEXT macro... I want to use the i to populate the %i value in the TEXT macro. – GSchian0 May 13 '22 at 16:39
  • if I don't change the name while creating the sub object unreal crashes and says I have a subobject with that name already. – GSchian0 May 13 '22 at 16:39
  • You can use somethine like: `std::string name = "Number" + std::to_string(i);` to get a string with a filename. You can try to pass it to `CreateDefaultSubobject` (or convert to another string format if needed). In addition you have to supply `CreateDefaultSubobject` with a `bool bTransient` param. See: https://docs.unrealengine.com/4.26/en-US/API/Runtime/CoreUObject/UObject./UObject/CreateDefaultSubobject/2/ – wohlstad May 13 '22 at 16:43
  • `RootComponent = CreateDefaultSubobject("SceneComponent"); FVector CurrentLocation = GetActorLocation(); StaticMeshes.Init(NULL,10); float distance = 0; // int32 i = 0; for(int32 i= 0; i < 5; i++) { FString Number = FString::FromInt(i); distance *= i * 100; StaticMeshes[i] = CreateDefaultSubobject(TEXT(Number)); StaticMeshes[i]->SetRelativeLocation(CurrentLocation + FVector(0.0f, 0.0f, distance)); StaticMeshes[i]->SetupAttachment(RootComponent); }` yields result "Cannot resolve symbol 'LNumber'" in the text macro box – GSchian0 May 13 '22 at 16:48
  • I'm not sure what type of string I need to convert it to, I've tried a few so far. – GSchian0 May 13 '22 at 16:52
  • Sorry, I am not familiar with Unreal in general and the method `CreateDefaultSubobject` in particular. Just tried to decipher the maning of the error messages you posted. – wohlstad May 13 '22 at 16:54

0 Answers0