In my header file, I have a weak pointer to an object of type UDuelComboWidget (which is derived from UUserWidget).
protected:
TWeakObjectPtr<UDuelComboWidget> ComboWidget;
In my .cpp file, I have the following code that executes whenever the user clicks the left mouse button.
if (!ComboWidget.IsValid())
{
ComboWidget = Cast<UDuelComboWidget>(CreateWidget(GetWorld(), ComboWidgetClass));
if (ComboWidget.IsValid())
{
ComboWidget->AddToViewport();
}
}
else
{
ComboWidget->RemoveFromViewport();
}
The issue I’m running into is that AddToViewport() is only called the first time the user clicks. Every other time, we hit the else part of the if statement. I was under the impression that if I called RemoveFromViewport() on this widget, and if there were no other strong references to it (hence the weak pointer), ComboWidget would automatically be set to be nullptr. However, ComboWidget never becomes nullptr unless if I explicitly set it to nullptr when I call RemoveFromViewport(). Is my understanding of TWeakObjectPtr incorrect?