Getting component by name

I want to get specific UMeshComponent by name. It appears that the only way to do this if iterate through all of the UMeshComponent and compare name for each one. This seems extremely inefficient for large environments. I read somewhere that this is because engine internally maintains list as linked list and so iterating whole thing is the only way.

Has this changed in newer versions? Is there a better way?

Thats why we usually refference things by a actuall refference =) is there a reasson why you “have to” look it up by Name? And if it is really neccessary to “search” for it you are heading into the territory of Allgorithms. Iterrating over all existing Components is like you said expensive. Ask yourself if you can reduce the Set you iterrate trough by predefining it in advance or if you know the Actor that holds the Component you just have to iterate those and not all that exist, which is significantly faster =)

Anyway there is no specific Solution for this vaguely described Problem you Face. But I hope that I have steered you in the right direction and you rethink if you actually have to solve the Problem by Name Search.

Good Luck and have Fun =)

1 Like

Try with GetDefaultSubobjectByName

Example:

UCameraComponent* ThirdPersonPlayerCamera = Cast(GetCharacterOwner()->GetDefaultSubobjectByName(TEXT(“FollowCamera”)));

GetDefaultSubobjectByName is a function embeded in all UObject classes and lets you get a pointer to an object whose name you know and search for in the TEXT(“objectname”) parameter…

In your case

UMeshComponent * MyMesh = Cast(GetDefaultSubobjectByName(TEXT(“Mesh”)));

hope this helps!

6 Likes

thanks, in my case working

UStaticMeshComponent* Trolley = Cast<UStaticMeshComponent>(GetDefaultSubobjectByName(TEXT("Trolley")));
3 Likes
FindObject<UMyComponent>(MyActorPtr,  TEXT("ComponentName"));
1 Like