Hi there!
I’m fairly new to UE and I’m wondering what’s the best way to have a reference to a specific instance of a component, when the Actor has several instances of that component class.
Specifics, if that helps :
My level has Rooms in it, which are a C++ Actor subclass (because I have a lot of generic logic related to them) with several Blueprint instances (because each Room has its own layout).
In those Rooms, I have Spots where I can place Items. Each Room can have any number of Spots.
It seemed like a good idea to have the Spots as components, because again a lot of the logic is tied to the Room (which Items are in a Room, how many Spots are free in a Room…).
class USpotComponent : public UStaticMeshComponent { /* ... */ };
class ARoom : public AActor
{
virtual void BeginPlay() override;
TArray<USpotComponent*> Spots;
};
void ARoom::BeginPlay()
{
Super::BeginPlay();
GetComponents<USpotComponent>(Spots); // Collect all Spots so I can use them in Room logic
}
My issue is that in my Item Actor, I would like to select the Spot where this Item is placed at the beginning of the game, but I can’t get a direct reference to a Spot component.
class AItem : public AActor
{
/* ... */
UPROPERTY(EditAnywhere)
ARoom* StartingRoom = nullptr;
UPROPERTY(EditAnywhere)
USpotComponent* StartingSpot = nullptr;
};
The StartingRoom property lets me pick any Room in my level, as expected, but I can’t pick a StartingSpot (it shows a drop-down where I can pick the component class but that does nothing…).
Is there any way to do this?
I have considered using Child Actors but I read a lot of negative feedback about using them and I’m not sure I can safely keep a reference on a spawned Child Actor.
Thanks a lot for your insight!