How do I find a component by name?

I want to define a position within my Pawn actor for something to float in front of the actor. It doesn’t have to be a rigid attachment like a socket, so I don’t mind if it is slightly off every frame.

Firstly I can’t find any function such as GetComponentByName()

Secondly, i’ve tried using some of the components that let you transform and rename them, such as an ArrowComponent or a SceneComponent, and changing their name. I can’t work out how to retrieve that name in code! I’ve tried GetName() but this doesn’t match the name I’ve edited, and looking through the header files I can’t find the variable that corresponds to the set name.

Can anyone help? Am I doing this wrong?

EDIT: It seems that there is no inbuilt way to do this, and the only way to do it is to create your own components or to retain a link to the component when you create it.

you can make your own component, and give it the name variable you want,

wiki tutorial: Custom BP-friendly UObject Components

then you can find it by doing this

for(TObjectIterator<UYourCompClass> Itr; Itr; ++Itr)
{
   if(Itr->YourStringVariableName = "YourName")
  {
     //do stuff!
     return;

  }
}
1 Like

Thanks , I ended up adding a new component in C++ which had a set name, and then just referencing that.

I did run into a problem adding a new component (in code) to a blueprinted objected I had already created - because the new component didn’t show up. I believe this is a known issue and has been fixed (?) in later builds. Anyway, If anyone else comes across this I ended up having to re-create my blueprint and manually transfer all of the old values I had to make the component show up.

When I want to find a component of a certain actor instance, is TObjectIterator the right choice? Doesn’t it catch all objects of UYourCompClass of all actor instances?