Get Component of Actor via UClass access of component

I am implementing a saving system and am serializing the components of an actor.
To restore the components, I need to know which saved component responds to which component of the actor I am trying to restore.

Example: Actor A has Components B and C.
I am saving Component B and C and know which class they are from.
When I want to restore Components B and C, I do not know programatically which of the two components I saved is which, hence I can not deserialize the saved components.

I simply tried using actor->FindComponentByClass and giving the class I saved as parameter, so I could get access to the now default-value components newB and newC of the freshly spawned actor, so I could then update the newB and newC components with the B and C components I saved.

However, all component access functions I could find require a template class, and not a UClass parameter. Obviously, components B and C are of unknown class, so I can’t just type the class into the template

How do I get a component of an actor (or even all components of a type) when I only have the UClass of the components available?

Hi !
GetComponentsByClass support what you need i think :

AActor* myActorToLoad;
UClass* componentClassToLoad;

auto component = myActorToLoad->GetComponentsByClass(componentClassToLoad);

Yep, that worked fine. I guess I overlooked that function. :slight_smile:

Hi, dose this function can only return UActorComponent*? My code dose’t work.

USCommondComponent* CommondComponent;
TeamCharacter[0]->GetComponentsByClass(CommondComponent);

"I think there is an error in this code. Only I can not figure out the error. Has anyone figured out this code? If there is a specialist who understands the code, write to me. I will need help with the code for the site https://writeanypapers.com/ on which I want to implement an interesting functionality that is not found on any of the writing services.

 USCommondComponent* CommondComponent;
 TeamCharacter[0]->GetComponentsByClass(CommondComponent);

Had similar issue, check this
https://forums.unrealengine.com/development-discussion/c-gameplay-programming/1357727-how-to-use-getcomponents-with-tsubclassof