Download

Override BeginPlay() defined in C++ with the BP child's BeginPlay()

Suppose I have a simple C++ class like:



public:
    virtual void BeginPlay() override;




void ATD_Character::BeginPlay()
{
    Super::BeginPlay();
    Print("I am the parent");
}


And I want to override it in a derived Blueprint like this:

Both the parent and child’s BeginPlay are called. How can I prevent this? Setting up what seems the be a parallel example using a BP for both the parent and child yields the desired result. Am I missing some macro or keyword to allow it to be overridden?

You can create functions that blueprints can override, but not for built in stuff like BeginPlay() as far as I know. But why would you override it?

If the goal is just to prevent the parent’s code of being executed, then why not using a simple boolean that you can set in the editor or in BP:



void ATD_Character::BeginPlay()
{
    // Call the base class
   Super::BeginPlay();

   if (!bExecBeginPlay) return;

   // rest of the code
}


You can also just have the parent’s BeginPlay function call a virtual function called “PrintWhoIAm.” Define the “PrintWhoIAm” function in the parent to print “I am the parent,” and then override that function in the child class to print “I am the child.”

You will get a lot of errors by not calling the parent implementation of BeginPlay().

You’re probably better off creating your own overrideable function that is called from the parents’ BeginPlay, and overriding that in the child instead.

Agreed - BeginPlay is the one thing you really shouldn’t be avoiding parent calls with.

Definitely better to create a function in the parent class, called on BeginPlay, that can be overriden in children.