Hi,
How to correctly keep a couple of logically related mesh components in my class? (think sides of a dice that I want to be separate meshes)
(In the example below I use a UCustomMeshComponent which inherits from UMeshComponent)
It did work for me either by doing:
UPROPERTY()
TSubobjectPtr<UCustomMeshComponent> SphereMesh1;
UPROPERTY()
TSubobjectPtr<UCustomMeshComponent> SphereMesh2;
//etc...
or
TArray<TSubobjectPtr<UCustomMeshComponent> > Sides;
However the latter one has some issues - for example cannot add an UPROPERTY() tag to it, as it crashes the engine (not being able to cast TArray to a property) and causes my custom meshes to disappear after I go to play mode and then exit from it.
The first one on the other hand is not very versatile…
Or maybe there is a better way to do it?
Thanks, great answer, that’s exactly what I needed.
Rama
(Rama EverNewJoy)
March 25, 2014, 9:30pm
3
#Static
if it is always 6 sides you can use a static array
UPROPERTY()
TSubobjectPtr < UCustomMeshComponent > Sides[6];
#Dynamic
Use a USTRUCT()
USTRUCT()
struct FSubObjStruct
{
GENERATED_USTRUCT_BODY()
TArray < TSubobjectPtr < UCustomMeshComponent > > Comps;
int32 GetCount() const
{
return Sides.Num();
}
//Constructor
FSubObjStruct()
{
}
};
//now you can use UPROPERTY()
UPROPERTY()
FSubObjStruct Sides;
#Ex:
Sides.Comps.Add(new subobj);
Why not create the array variable in the derived blueprint and make a function in the c++ class that takes the array in from the blueprint and then call that function in the blueprint?
AlexYopp
(AlexYopp)
October 6, 2015, 11:17pm
5
I’m attempting to do something similar, but my objects aren’t actually being exposed to the editor. When I try
UPROPERTY(EditAnywhere)
UPaperSpriteComponent* DoorSprite[9];
…the first object is exposed, but none of the rest are. If I instead try to expose them individually, none of them are:
for (uint8 i = 0; i < _NumberOfDoors; i++){
UPROPERTY(EditAnywhere)
DoorSprite[i] = CreateDefaultSubobject<UPaperSpriteComponent>(*(FString::Printf(TEXT("DoorSprite_%u"), i)));
DoorSprite[i]->AttachTo(Root, NAME_None, EAttachLocation::KeepWorldPosition);
}
were you ever able to find a solution? I am facing the same issue, can’t have a dynamic array of exposed meshes.
Thumper
(Thumper)
November 1, 2016, 2:32pm
7
TSubobjectPtr is deprecated so this whole answer no longer works. What would be the correct way to do this now using CreateDefaultSubobject?