# Get attached actors by class

**URL:** <https://forums.unrealengine.com/t/get-attached-actors-by-class/1915525>\
**Category:** Programming & Scripting\
**Tags:** actor, child-actors, unreal-engine, question, CPP\
**Created:** [June 25, 2024, 2:31am UTC](https://forums.unrealengine.com/t/get-attached-actors-by-class/1915525 "2024-06-25T02:31:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaNiHa7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MaNiHa7](https://forums.unrealengine.com/u/MaNiHa7)\
**Post date:** [June 25, 2024, 2:31am UTC](https://forums.unrealengine.com/t/get-attached-actors-by-class/1915525/1 "2024-06-25T02:31:44Z")

</div>

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?

```auto
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.

---

<div class="post-metadata">

**Author:** ![silnarm](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/silnarm/32/955303_2.png) [@silnarm](https://forums.unrealengine.com/u/silnarm)\
**Post date:** [June 25, 2024, 3:03am UTC](https://forums.unrealengine.com/t/get-attached-actors-by-class/1915525/2 "2024-06-25T03:03:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MaNiHa7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MaNiHa7](https://forums.unrealengine.com/u/MaNiHa7)\
**Post date:** [June 25, 2024, 3:09am UTC](https://forums.unrealengine.com/t/get-attached-actors-by-class/1915525/3 "2024-06-25T03:09:02Z")

</div>

Thanks, got it.

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

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [July 25, 2024, 3:09am UTC](https://forums.unrealengine.com/t/get-attached-actors-by-class/1915525/4 "2024-07-25T03:09:42Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
