Properly binding function to Slate text?

So I’ve been trying to learn Slate recently, and I guess the next step is to update data during gameplay (rather than keep it as it was when it was initialized), so I tried:

-Passing a function pointer (the address of the function, namely &SMyWidget::GetValue style syntax)

-Creating a delegate:

DECLARE_DELEGATE_RetVal(FString, fstringdellegate);
	fstringdellegate del;
	del.BindSP(this,&SMyWidget::getFocusedItemNameS);

-Using TAttribute, which actually seemed to be the most reasonable to me:

TAttribute<FString> Value = TAttribute<FString>::Create(TAttribute<FString>::FGetter::CreateUObject(this, &SMyWidget::getFocusedItemNameS));

In each and every caseI got errors related to syntax generally. Here’s where I want to put the text:

.Text(this, /*can you get a bleeping update-able value in here??*/)

So, is there any hope? Can anyone provide an appropriate syntax to match either of these methods to get that variable in there? I’d be delighted to have an answer for each method, but I’ll settle for one that works.

Many thanks in advance, cheers!

Edit: The structure is as follows:

MyWidget.h:

public:
	SLATE_BEGIN_ARGS(SMyWidget)
	{}
	SLATE_ARGUMENT(TWeakObjectPtr<class AMainHUD>, OwnerHUD)
	SLATE_END_ARGS()

	/** Constructs this widget with InArgs */
	void Construct(const FArguments& InArgs);
private:
	TWeakObjectPtr<class AMainHUD> OwnerHUD;

	AMyProjectCharacter* character;

	FText getFocusedItemName();
	FString SMyWidget::getFocusedItemNameS();

I set the character through this sequence, which has so far brought back the right strings, so it should still be accessing the only character:

TArray<APlayerController*, FDefaultAllocator> controllers;
	GEngine->GetAllLocalPlayerControllers(controllers);
	character = (AMyProjectCharacter*)(controllers[0]->GetPawn());

From here on, I’ve got a function returning FString, and a near identical one returning FText (I made sure they return the right thing and their names are different) getting a string from the character, which changes during gameplay.

Initialization of the widget works perfectly fine, I’m only changing the text it displays.

Can you post more of the code? I would like to see the two classes (the widget, and whatever contains the widget) so that I can offer you better advice.

Well, what contains the widget is pretty much empty other than the declaration and initialization of the widget, and other than what I’ve added with the edit, the widget only contains the functions to retrieve strings from the character and the script that places the text at the top of the screen, which is long and works, so I believe it would just be a waste of space to add that too…

Alright, well, hope you’re able to figure it out on your own. Good luck!

I got it working with the TAttribute, which sounds reasonable. To the template you pass the return type, then as a parameter to create you use CreateRaw from the FGetter member of TAttribute, where you give it the instance of the class from which to take the function, and then the function itself, which must be specified based on the class (uh, there’s probably a better term for that), so don’t forget the “SMyWidget::” part before the function name. So the declaration was like this:

TAttribute<FString> Value = TAttribute<FString>::Create(TAttribute<FString>::FGetter::CreateRaw(this, &SMyWidget::getFocusedItemNameS));

And I simply used it like this, which beats the whole LOCTEXT procedure:

	.Text(Value)
2 Likes