Hello, I’m here with a “best practice” question for Slate.
Say I have a widget I’m creating (let’s call it SMyWidget) and in the construct I’m using several dozen STextBlocks. Every time I create an SMyWidget, the construct for it is calling numerous SNew(STextBlock) and let’s assume the .Text for these STextBlocks is always the same.
Does it make sense to cache the TSharedPtr for these somewhere and reuse them?
I’m over-simplifying the usage in the example, in reality these STextBlocks are used in various SComboBoxes and as labels but they are using a defined set of .Text values.
I’m still getting familiar with Slate and I’m not sure if the amount of SNew churn that is generating and throwing away all these various SWidgets is something I should worry about for garbage generation and cleanup and if pooling makes sense at all with Slate.
Also worth noting this is for an editor-only widget.
Example of kinda what I’m thinking:
//In some global access point:
TMap<FString, TSharedPtr<STextBlock>> TextBlockCache;
TSharedPtr<STextBlock> GetCachedTextBlock(FString Key)
{
if(TextBlockCache.Contains(Key)
{
return TextBlockCache[Key];
}
TSharedPtr<STextBlock> NewTextBlock = SNew(STextBlock).Text(FText::FromString("Key"));
return TextBlockCache.Add(Key, NewTextBlock);
}
//In various custom widgets:
void SMyCustomWidget::Construct(const FArguments& InArgs)
{
TSharedPtr<StextBlock> MyTextBlock = GlobalAccessPoint->GetCachedTextBlock("Some value");
ChildSlot
[
MyTextBlock
];
}