Add custom component during runtime

Hi. I’m working on an rpg combat system, and I want to create a function which adds buffs to character. Buffs are actor components. The problem is, “add component” only adds a component of certain class, and my function has to be able to add any buff I want, so class reference must serve as input. I can’t find a node which receives class input and then adds a component of that class. I also tried “construct object from class” node, but it seems not to work with actor components. Is there a way to do this using blueprints?

edit - scratch that, I misunderstood you

Edit - Tried adding them through a dedicated component, but then it doesn’t allow you to add components that way unfortunately.

Any ideas?

There are two approaches that I know of.

1: Make your component parented by Object and use Construct Object From Class. Not the greatest, but offers flexibility in spawning exactly what you are hoping to spawn. This is ideal in scenarios where you want custom functions inside of each child. I’ve not tested this approach and handling it as a component – you’d be on your own in that regard. I’ve seen others posting this solution and using it in this scenario, so I’ve made the assumption that it works.

2: Create a parent component. Setup all the logic inside of it so you only need to change exposed variables to get the functionality you’re looking for from each child. Create the children with variables set accordingly. In your logic responsible for adding the component, add the parent (not child) component. Whatever is adding this component to your character needs to store the child class. Use that child class to Get Class Defaults and plug those variables into the logic that’s spawning the parent. It’ll spawn the parent, but it’ll set the variables to the variables of the child. This is ideal when you only need variances which are provided by variables themselves. Health, armor, attack, damage over time, whatever.

^ In this example, I’m spawning the parent (Shooter), and using class defaults from the child (which is dynamic, any powerup I grab will have a different child set). It works totally fine for these purposes, but of course, do note that any custom functions added inside the child will absolutely not be accessible with this approach.

The second approach requires that every time you change exposed variables, you have to plug in anything new. It’s manual and kind of sucks, but it allows dynamically adding/removing components.

A third option is doing it in C++. There don’t seem to be the same limitations with C++, and in particular I think Construct Object From Class actually functions with components in C++ whereas it errors in Blueprints. I could be wrong on that as I’ve not tested it.

One Mode Only, thanks for a detailed response.

I am currently working on solution 1, and I’m missing functions like “Get component(s) by class”, “Get component by tag”, but for child objects. I need my bp interfaces to be able to trigger some events inside the effects currently applied on a certain actor, also need a way to determine if a certain effect is already applied.
Also I heard objects can’t tick, but I think that won’t be a problem.

Any further help would me much appreciated!

On a second thought, I think I can write such functions (like get component) myself.