Attaching multiple components to same component or actor?

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
{
};

all actors need a default root component…when you create an actor it has
a default Scene component ( USceneComponent in C++) This default Scene component is like a null root object (it only holds transforms).

image

If you attach Components to Actor you are actually attaching to that root Scene component so I think Attach component to actor is the same as attach component to scene root actor component like this:

image

you can also replace the default roo componenbt with any other like this:

image

Best,
Dany

2 Likes

Thank you a lot! so it’s possible, now I only have to figure out how to do this in C++

If anyone has an idea how it is done in code I would be grateful for that.

EDIT:
OK, I figured it out for C++ portion, thank you a lot, the trick was indeed to attach to scene component!

1 Like