I’ve been reading trough engine code regarding attaching component to component and it seem attachment is hierarchical…
Say you have components A, B, C and an Actor called Actor, then the attachment is as follows:
Actor
→ A (attached to RootComponent of the Actor)
→ -> B (attached to A component)
→ -> → C (attached to B component)
Where C is attached to B, B is attached to A and A is the Actor’s root component (attached to Actor).
What I would like to know whether is it possible to attach A, B and C components directory to RootComponent of the Actor?
Therefore it would look like this:
Actor
→ A (attached to actor)
→ B (attached to actor)
→ C (attached to actor)
That is all A, B and C are attached to Actor’s RootComponent directly instead of being attached to each other.
Is my conclusion correct that this is not possible?
Is there anything I can do to attach components directly somehow?
My use case is that I whish to create a Map which will contain multiple square tiles, the map is an Actor and all the map tiles are either components or Actors attached to the map Actor therefore all tiles must be childs directly connected to map.
Example map generator class:
UCLASS()
class AMapGenerator : public AActor
{
GENERATED_BODY()
public:
AMapGenerator();
TArray<ATile*> MapTiles;
};
AMapGenerator::AMapGenerator()
{
PrimaryActorTick.bCanEverTick = true;
UWorld* World = GetWorld();
static bool once = false;
if (World && !once)
{
once = true;
FRotator rotation(0, 0, 0);
for (int x = 0; x < 3; ++x)
for (int y = 0; y < 3; ++y)
{
FVector location(x * 100, y * 100, 0);
FString TileName = FString::FromInt(location.X) + FString::FromInt(location.Y);
int32 elem = MapTiles.Add(World->SpawnActor<ATile>(location, rotation));
// This is a problem line because there is only one root component
// but we have many tiles
MapTiles[elem]->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform, *TileName);
}
}
}
ATile class is the actual Tile on the map which can be either an Actor or PrimitiveComponent, here is constructor only:
UCLASS()
class ATile : public AActor
{
};