Override to function RemoveFromParent | How to play animation before widget removes from parent?

Hi,

I would like to override RemoveFromParent function in my subclass derived from UUserWidget.
But suddenly UE4 started to crash :slight_smile:

What could be the reason?

/* here is my signature */
virtual void RemoveFromParent() override;

/* here is my function */
void RemoveFromParent
{
   // do something something.
   Super::RemoveFromParent();
}

The reason behind this, I want to have fadeout animation in general before removing widget from viewport. If any suggesstion to make it, Its also welcome.

You’re missing the parameter paranthesis, and the class your function belongs to in the example you posted. (Not sure if that would even compile if that’s actually the case and not a typo though?)

void MyUserWidget::RemoveFromParent()
{
   // do something something.
   Super::RemoveFromParent();
}

Also, virtual is only needed if your new function is going to be overidden by a child of it’s own. :slight_smile:

You can’t play an animation when the thing is removed. Removing the thing is a direct action.
Instead, the way to do this, is to add an animation notify at the end of the animation.
Tell the widget to play the “go away” animation, and inside the animation notify callback, remove the thing.

This isn’t going to work the way you want it to. RemoveFromParent happens immediately. While overriding it does give you a change to do something that something also has to happen immediately. Playing an animation is not immediate.
Your best solution would be a custom function that when you call it the fade is started and when the fade completes the widget would remove itself from the parent. Whoever is managing the widget would call this custom function instead of RemoveFromParent. Obviously this won’t help when it’s being removed by some UE4 process, but there’s not really anything you can do in that situation.
Maybe this is related to your crash if the widget has been gc’d but the animation is still trying to run on it. I’m not sure.

FWIW this is not true. Once it’s been declared virtual it is override-able by any subtype regardless of the override declarations. Personally I omit the virtual keyword from all my overrides. The only thing that affects the ability of a subtype to override a virtual function is the final keyword which prevents any subtype from overriding that function any further.

why do we have event destruct in UUserWidget. As far as I realized, its not also being triggered by remove from parent function.