HowTo: Find Component Entities by Name, ID, Favorite Food

You’re taking your new components for a walk in the park one afternoon and suddenly you look around and can’t find them. What do you do?

  1. Grab the ComponentApplicationBus header
#include <AzCore/Component/Entity.h>
#include <AzCore/Component/ComponentApplicationBus.h>
  1. Enumerate!
    EBUS_EVENT(AZ::ComponentApplicationBus, EnumerateEntities,
[this](AZ::Entity* entity)
// perform the condition here (check the name, find a component etc.)
// you can use the AZ::Entity::FindComponent methods to look for a particular component type – AzCore\Component\Entity.h if (strcmp(entity->GetName().c_str(), "MyEntityName") == 0) {
// do something with this entity
  1. Relax. You earned it.

If you already know the ID of your entity you can use the FindEntity method like so:

    AZ::Entity* entity = nullptr;
AZ::EntityId entityId; // set to your Entity ID
EBUS_EVENT_RESULT(entity, AZ::ComponentApplicationBus, FindEntity, entityId)
if(entity != nullptr)
// do something awesome.
1 Like