Confusion regarding TEXT(), printf() and String Formating!

Hi!

I have noticed a few different outputs in the Screen Log messages (in Lecture 19 UE Multiplayer C++ course). The code in the lecture is working properly, however, I was experimenting with some other methods.

Firstly, when I tried using regular FString Parameter in Host Method, it was giving compilation error.

Secondly, when I tried only TEXT() Macro written as below, it did work but was only printing the Parameter and not the “Hosting!” which was there in Text Macro.

image

I am not able to figure out why? And hoping to get some explanation regarding the concept of printf() and reference values.

You’re missing * for the argument to Printf. Printf needs C-style strings which is what the * on FString converts it to

Because what you have there is use of the comma operator. As the fourth argument to AddOnScreenDebugMessage you have

(TEXT("Hosting %s"), ip)

The comma operator evaluates both expressions and returns the right hand side e.g.

int value = 2,3; // value == 3

So what your fourth argument actually is, is just ip.


The TEXT macro is for changing the type of string literals depending on platform as different platforms use different character types to handle Unicode.

A literal being literally typing a value e.g.

42      // int literal
3.14    // double literal
'c'     // char literal
"hello" // string literal

You can control the types of literals with prefixes/suffixes

42u      // unsigned int literal
3.14f    // float literal
L'c'     // wide character 
L"hello" // wide string literal
// "wide" being the type wchar_t

So on Windows TEXT prepends an L on others nothing or something else.

FString::Printf uses the printf function from C to create an FString. As it’s from C it only deals with the primitive types which FString is not.
C deals with strings as a null terminated character array, it will keep reading the next character in memory until it reaches the null character and then it stops.
FString::operator* is a way to get the pointer to the first element in that character array.

Ok! Thanks for the explanation…

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.