Virtual function override

Ok, so what I’m trying to do should be simple.
This is a blueprint-only project.
I have a Character class and two derived classes: Player and Enemy.
In Character I have CheckDeath function (implemented) and a Die function (virtual). CheckDeath should call Die from Character blueprint, and then each derived class should handle the call with different implementation each. The problem is, when I call Die from Character class, it doesn’t call Die from subclass, but from itself. I tried making it pure virtual, but then I can’t seem to execute it from Character.
Now I’m trying to use Dispatchers, but feels like I’m overcomplicating something that should be simple.

Any thoughts?

Sounds like you need 3 functions CheckDeath, CanDie and Die.

CheckDeath calling CanDie and then if true Die. All in the Character Class

Then in Player and Enemy you can override the CanDie function.

Every function in Blueprint is virtual and can be overridden btw.

I’ll try to better explain the problem. This is the piece of code in question.

This event calls CheckDeath… CheckDeath just checks if Health is lower than zero and then calls Die, which should be implemented in derived classes.

At the moment, Die just print something.


And the result…

That means the function Die from Player is not being accessed, even though Player is derived from Character. If I try to make Die pure virtual, I cannot call it from Character blueprint for some reason.

And that’s it. Am I doing something wrong? Any ideas?

Pure function are functions that should return something since they are evaluated at every call. Pure functions should not have any side effects potentially changing the return value for every call unless if the input parameter is different.

The purity of a function has nothing to do with inheritance, however it is a bit odd that “Char Die” is being called and not “Player Die”. Are you sure that you don’t have a CharacterBP instead of a PlayerBP placed in the Level?

What other options are not default in the class and functions?

Perhaps try and recreate the behavior in two new Blueprint classes and see if the problem persists.

omg, I feel SO dumb. You are absolutely right, everything is working as expected now.

I thought “pure” meant C++ pure virtual functions. I’ll read more about this.

Thank you very much GarnerP57! You saved my day.

Glad you solved it, it is a classic mistake we all make making wrong assumptions as to where the issue is which results in tunnel vision. Sometimes you just need to take a step back and go through everything.

The main difference between pure and impure functions is that impure functions have to have their execution pin called while pure functions don’t have execution pins and is called once for every node it is connected to.