I have a UWidget class wrapping a set of Slate widgets (SBox → SButton → STextBlock). The SButton is getting a custom FButtonStyle that I define in the UWidget’s RebuildWidget().
The issue is, sometimes I get a null pointer crash when I click on the button, and sometimes I don’t. When I replace my custom FButtonStyle with a premade one, the crash goes away, so I know that the issue is in my implementation of the button style.
I think the issue is that I’m defining the FButtonStyle as a local variable and telling the widget to reference that variable, leading to a dangling reference. I’m unsure of how to allocate the FButtonStyle on the heap though, since I can’t just use the “new” keyword and make the variable a pointer.
TSharedRef<SWidget> UForestObject::RebuildWidget()
{
const FButtonStyle NoBorder = FButtonStyle()
.SetNormal(FSlateNoResource())
.SetHovered(FSlateNoResource())
.SetPressed(FSlateNoResource())
.SetNormalPadding(FMargin(0, 0, 0, 0))
.SetPressedPadding(FMargin(0, 0, 0, 0));
TSharedPtr<SBox> MyBox = SNew(SBox)
.WidthOverride(250)
.HeightOverride(50)
[
SNew(SButton)
.ButtonStyle(&NoBorder)
.OnClicked(BIND_UOBJECT_DELEGATE(FOnClicked, SwitchOnClick))
[
SNew(STextBlock)
.ColorAndOpacity(FLinearColor::White)
.Font(FSlateFontInfo("Roboto", 22))
.Text(FText::FromString(DisplayName))
]
];
return MyBox.ToSharedRef();
}
The end goal is to get a NoBorder button style that doesn’t move when clicked (unlike the premade “NoBorder”). How would I go about doing this properly?