Create and attach a component from a UClass

Hi there guys, i’m trying to make a power up system, in wich the character can attach a component of certain type.

UFUNCTION(BlueprintCallable, Category = "Vertex Character", meta=(Tooltip="Absorb the given essence into the character"))
	virtual void AbsorbEssence(TSubclassOf<class UEssenceComponent> EssenceClass);

This is made so any blueprint powerup can call the AbsorbEssence with the type of the EssenceComponent that i want

The problem is that i given this TSubclassOf Object (UClass) i can’t attach the component or create it.

I tried doing:

essenceComponent = EssenceClass->GetDefaultObject<UEssenceComponent>();

But the engine crashes because something something about the world of the actor not existing. (its a ensure that RegisterComponent does that my component fails).

I’m banging my head here, any idea how can i dinamycally add these components to my actor given the UClass?

Check this answer

Maybe there is a problem on how you are constructing the essenceComponent object

Just one question, ConstructObject is deprecated, but it’s the function that allows me to specify a UClass parameter apart from theTemplate return of the function, does NewObject have something like that?

Yup the NewObject function’s second parameter is exactly that