Download

Slate: I need somebody to hold my hand

Maybe I’m an idiot, but I simply cannot figure out Slate.

I can draw an FString of predefined text to the screen and move it around, so I guess I know a little bit about Slate, but I am completely lost when it comes to changing that text.

I have an actor in the world that keeps track of it’s own internal variable, which increments pretty often. Instead of spamming the log with this value, I’d like to print it to the screen. I’ve been trying to figure this out for two or three days now, and I have made absolutely zero progress.

My questions are:

  1. I know that this is going to require a “delegate”, and I think that has something to do with either a SLATE_ARGUMENT or a SLATE_ATTRIBUTE. Args are for variables and attribs are for functions, right? I could either pass the value from the actor in the world to the HUD, or I could, from the HUD, call a function within the actor to return the value. Which would be best? Which would be the most simple? I’ve already asked about this on the AnswerHub, but that honestly did more harm than good because I’m even more confused now than before I asked.
  2. Since Slate works with FText, and the value I’d like to display on screen consists of both static words and dynamic numbers, how do I concatenate things with FText? I’ve looked at FText::Format, but I can’t get it to work without LOCTEXT and I can’t figure out how to set up a LOCTEXT_NAMESPACE
  3. Slate is better than Canvas, right? Is there a good reason for me to be repeatedly bashing my head into this wall, or should I just salvage what I can from my old UDK Canvas code? Is there any performance gain with Slate?

Thanks in advance.

A Slate argument will only accept a value, while an attribute will accept either a value or a function. Use arguments for values that don’t change, and attributes for values that do. If you assign a function to an attribute that function will be called by Slate periodically to obtain the current value of the attribute. As for the HUD, I’d try pulling data from the Actor, rather than pushing data from the Actor to the HUD.

You generally define LOCTEXT_NAMESPACE at the top of your source file (after the #includes), and undefine it at the bottom of your source file. Here are a couple of convoluted examples of FText::Format:



#define LOCTEXT_NAMESPACE "MyNamespace"

int32 PlayerID = 1;
FText MyText = FText::Format(LOCTEXT("HitMessage", "Player {0} was hit!"), FText::AsNumber(PlayerID));

FString PlayerName(TEXT("John"));
int32 PlayerPoints = 500;

FFormatNamedArguments Args;
Args.Add(TEXT("PlayerName"), FText::FromString(PlayerName));
Args.Add(TEXT("PlayerPoints"), FText::AsNumber(PlayerPoints));
FText MyText2 = FText::Format(
    LOCTEXT("PlayerStats", "Player: {PlayerName}
Points: {PlayerPoints}"), Args
);

#undef LOCTEXT_NAMESPACE


Whether Slate is better than Canvas or not depends on what features you need. Slate has layout containers, and all kinds of widgets necessary to build a complex UI, while Canvas as far as I’m aware really just draws boxes, lines, and text (which may be just fine for a largely non-interactive UI like a HUD).

The best way to learn Slate is by looking at the UnrealEd source, there’s a Widget Reflector tool (Window->Developer Tools) that you can use within UnrealEd to figure out where each widget you see is implemented. There is also a Widget Gallery of some sort, though I haven’t figured out where it’s located yet.

It’s the SlateViewer app, which is in Engine/Source/Programs/SlateViewer. Be aware that it asserts/crashes in some portions.