Download

How to override the functions in a component attached to a player character?

Other than creating a child class of the component and overriding the functions in the child class, that is.

I do not wish to edit the original component as it is a purchased asset and I am trying to maintain it in factory condition to allow easy updating.

Also, don’t know of a way to utilize functions from the child player controller class (which I need for modifications and additions) in the parent player controller class. It seems to me that this would be impossible.

I would prefer not to create a new class for the component, as my player controller class inherits a large amount of code from its parent, in addition to the inherited component. As I am looking at it now, creating a component child class would require me to create an entirely new player controller (essentially rebuild the parent) class to use the child component.

I could add the new component child class to my existing PlayerController, but then I would have two of the components (one inherited.)

I am looking for a solution to this same issue.
Did you have any success? :slight_smile:

This seems to work, but hopefully there is a more optimal solution since for each component implemented by a bp it would require 2 components:
MyComponent (for the default behavior, not implemented by bps)
MyComponentChilld (overridable, implemented by bps)

You dont need to add both components when you override functions.

And yes overriding functions is the normal way to modify the functionallity of a component.
If needed i can add a example from my Projects.

Hi Synia,

Sure it would be appreciated :). I’m trying to find the best pipeline before getting my hands dirty.

By now from this post I found a solution, basically it means:

  1. Add an event dispatcher to your component.
  2. Implement that component in your actor.
  3. In your actor blueprint now you can implement the events from the event dispatcher.
  4. In your component you can create events/functions for a default implementation.

The picture is my resource component from here(im using it for what ever resource i want -> health, shields, mana etc.).
On the left is a Get function where i return just the base Regeneration delay to use it in other functions.
On the right is the overriden function in a child component, because i wantet a way to get my base values from an inventory. If the inventory is not valid, i use the parent function.
All functions that aren´t overridden work like the parents function.

I dont have an example for the damage calculation because that is managed by another component and is pretty large.

I would not use a Dispatcher! I use dispatcher only if different actors or functions have to listen to a specific event, e.g. my dispatcher here are ResourceValueChange and ResourceReachedZero.
Its possible to do it with dispatcher but in my opinion it is not elegant.

If you have questions just ask.

As far as I know, if the component is created in C++ and it has blueprint native/implementable events you can override the event functions in a blueprint class that inherits the C++ class as a parent. Otherwise you can’t override C++ functions in blueprints directly.

I can say the same.

The desired functionality would be (just an imaginary example):
I create a bp based in actor-component: “HealthComp”, where I have a float with the value for health and a function to set it, Increase or Decrease it, etc.
I create a “bp_soldier”, add the component Health.
I create a “bp_dragon”, add the component Health.

Soldier would do a different thing than Dragon when they DecreaseHealth, so they would override DecreaseHealth.

The only alternative I found was to use 2 components to be parent-child, which I think is even worse, so I would have:
HealthDefaultComp
HealthComp (inheriting from HealthDefaultComp)

Soldier and Dragon would implement “HealthComp”

If you have a custom C++ actor component called “HealthComp” and it has blueprint implementable or blueprint native events contained in the base C++ class you can’t override them by adding the C++ component “HealthComp” to any actor. What you need to do is in the content browser create a new blueprint class. Choose “HealthComp” to be the base class and then you will have a blueprint class “BP_HealthComp” which inherits from the parent C++ “HealthComp” class. Inside the blueprint version of the “HealthComp” you can now override any C++ defined blueprint implementable or blueprint native events. So when you add the “BP_HealthComp” to “Soldier” you can say to the soldier class specifically to do X when it takes damage, whereas the “Dragon” can have its own “BP_HealthComp” added to it and when it takes damage do Y.

You can do it exactly as you write.
The question is why you want to override the DecreaseHealth function?
Normally the function should only subtract a float value from the health value and that should be the same for humanoid and dragons.

Edit:

I don’t think he uses a custom C++ actor when he posts it in the blueprint visual scripting section

True…I just saw the original original post and saw “purchased asset” and assumed it might be C++ component. But either way, I think we are saying the same thing.

E.g.: If the dragon’s health goes bellow 50% it will trigger a cinematic.
*Normally I won’t override but I am going for a pipeline that would let me do it if needed.

So, in conclusion, the best solution here would be to use a component for the default behavior and another for each class?

If I would decide to implement these components via C++ what way would you suggest?

  1. Add C++ class “HealthComp” inheriting from actor-component
  2. Add C++ class “DragonHealth” inheriting from “HealthComp”

And then expose the functions like “DecreaseHealth” to blueprint?

Edit: Should the C++ classes be based in ActorComponent or they could be based in simple objects? Not sure If it is possible and if it is a good practice to add objects as components. (Will try just now).

Thanks in advance :wink:

I use my Dispatcher “ResourceValueChanged” for things like that, but the exact same thing can be archieved with overriding.
Just override the function, call the parent function and add your code for the cinematic or what ever.

You only need one component for each class, this component should be a child of your base component or the base component itself.

I’m not very good at C++, but you should not use objects, because they do not provide replication by default.

If you need a distinct component subclass for each actor class, then there isn’t much point in using components, is there? You’d likely be better off coding the default behavior in a generic Actor/Pawn/Character class (possibly abstract) and overriding it when needed.

Hi, sorry for bringing this back again, but this would let you override just in the BP_HealthComp and not in the soldier or whatever class implementing this component.

If I have:
BP_CharParent : Function DoSomething
BP_Soldier (child of BP_CharParent) can override DoSomething

If I have:
BP_CharParent : ImplementsComp BP_Health with Function DoSomething
BP_Soldier can’t override DoSomething

There is really no way to override components?

*An Interface would be perfect if I could add a default implementation to it, and also I could put some variables into it.