So in my project I have a UserWidget child class being used like so
if (UserWidgets[1] != nullptr)
{
OfficeDepMenuWidget = CreateWidget<UCeoDepMenuWidget>(UGameplayStatics::GetPlayerController(this, 0), UserWidgets[1]);
}
On Actor overlap I then use said defined & created widget, and add it to the viewport like so
void AOfficeDepartment::NotifyActorBeginOverlap(AActor* OtherActor)
{
if (Cast<AProjectIdleCharacter>(OtherActor) != nullptr)
{
if (OfficeDepMenuWidget != nullptr)
{
OfficeDepMenuWidget->AddToViewport();
}
}
}
Well this works, and I remove it like so when no longer in range
void AOfficeDepartment::NotifyActorEndOverlap(AActor* OtherActor)
{
if (Cast<AProjectIdleCharacter>(OtherActor) != nullptr)
{
if (OfficeDepMenuWidget->IsInViewport())
{
OfficeDepMenuWidget->RemoveFromViewport();
}
else if (BacklogWidget->IsInViewport())
{
BacklogWidget->RemoveFromViewport();
}
}
}
This also works. Here comes the problem. About a minute later when the garbage collector is triggered (RemoveFromViewport seems to call the GC), the components inside my widget seemed to get deleted as well. If I go inside the overlap again about a minute later, instead of Adding it to the viewport again I crash. (I can go in and out just fine as much as I want before this minute timer runs out before the GC comes to do its stuff)
The widget itself isn’t null, using debug stuff I can see that, it’s instead the components inside the widget that get null’d (Inside the widget blueprint, the components such as the textboxs, horizontal boxes, etc). It’s also pretty clear because in my BeginOverlap which is where I crash, I have a nullptr check.
What gives? I can’t figure out what I’m doing wrong. I need to be able to Show/Hide lots of widgets all the time in this game, and I don’t want to set the visibility of like 30 widgets to hidden if I can get away with removing it instead.
UE 4.24.3