Get attached actors by class

I have various actors of c++ class Room, each with their own set of attached child actor components of c++ class RoomExit. I want to get the list of RoomExits from a given instance of Room, and be able to use the functions I’ve written in RoomExit.cpp.

I can call GetAttachedActors() on a parent Room actor to correctly get a list of the attached exit actors, but these come as AActor pointers, not ARoomExit pointers.

How can I get this list of actors as their appropriate actor subclass?

TArray<AActor*> AttachedActors;
RoomActorInstance->GetAttachedActors(AttachedActors);
for (AActor* Attached : AttachedActors)
{
	ARoomExit* Exit = Attached;
	int dir = Exit->GetBaseDirection();
}

This is my non-working code, where I am not able to set Exit=Attached, because they are of different classes.

Just cast it,
ARoomExit *Exit = Cast<ARoomExit>(Attached);

Assuming you are attaching nothing other than exits, if there are other things attached (or might be later…) then be sure to check Exit before using it.

1 Like

Thanks, got it.

I had tried casting before but was stupid and did Cast<ARoomExit*> instead which obviously didn’t work.

1 Like