Calling a 2 Parameter Delegate from slate

Hello everyone…

My custom slate widget has 2 int variables.

say

TAttribute<int32> RowNum;
TAttribute<int32> ColNum;

I have a 2d array in HUD containing some values which are changed dynamically.

I want to get the values from the array using the 2 variables.

Say there is a FText present inside the array and I want to get its value at run time using the 2 variables and update it on my slate. Whats the best way to do it?

Hi,

Slate handles retrieving dynamic content for widgets intrinsically, so you should be able to (for instance, set up a text block that shows you the element) do something like this:

class SMyWidget : public SCompoundWidget
{
public:

	SLATE_BEGIN_ARGS(SMyWidget)
		: _RowIndex(0), _ColumnIndex(0)
	{ }
		SLATE_ATTRIBUTE(int32, RowIndex)
		SLATE_ATTRIBUTE(int32, ColumnIndex)
	SLATE_END_ARGS()

	void Construct(const FArguments& InArgs)
	{
		RowIndex = InArgs._RowIndex;
		ColumnIndex = InArgs._ColumnIndex;

		ChildWidget
		[
			SNew(STextBlock)
			.Text(this, &SMyWidget::OnGetText)
		];
	}

	FText OnGetText() const
	{
		return MyHUD->FindText(RowIndex.Get(), ColumnIndex.Get());
	}

private:

	TAttribute<int32> RowIndex, ColumnIndex;
};

You could optionally pass in a pointer to the array as well, so the widget isn’t tied to the HUD explicitly, if you can guarantee the HUD will outlive the slate widget.

I tried something like this

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SItemWidget::Construct(const FArguments& InArgs)
{
	GameHUD = InArgs._GameHUD;
	SlotLoc = InArgs._SlotLoc;
	
	ChildSlot
	[
		SNew(SBox)
		.Padding(5)
		.HeightOverride(100)
		.WidthOverride(100)
		[
			SNew(SBorder)
			.HAlign(HAlign_Center)
			.VAlign(VAlign_Center)
			.BorderImage(&GameHUD->EmptySlotImg)
			.Padding(FMargin(5,0,0,0))
			[
				SNew(STextBlock)
				.Text(this, &SItemWidget::GetNumOfStacks)
			]
		]
	];
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION

FText SItemWidget::GetNumOfStacks()
{
	return FText::FromString(FString::FromInt(GameHUD->StoragePtr->Storage.Rows[SlotLoc.Get().RowNum].Columns[SlotLoc.Get().ColNum].ItemIndex));
}

And I am getting this compiling error

1>C:\Users\Srikant\Documents\Unreal Projects\SurvivalGame\Source\SurvivalGame\Inventory\Slate\Widgets\SItemWidget.cpp(31): error C2664: 'STextBlock::FArguments::WidgetArgsType &STextBlock::FArguments::Text(const TAttribute<FText> &)' : cannot convert argument 2 from 'FText (__cdecl SItemWidget::* )(void)' to 'FString (__cdecl SItemWidget::* )(void) const'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Corrected just added a const