How to print string with append node in C++?

Hello, I want to print string in C++ using the same method as in this blueprint code.


How can I achieve this? Thank you

what I am doing is :smiley:
I don’t want to use kismet library

FString StrRet1{};
FString StrRet2{};
FString StrRet3{};
FString StrRet4{};
FString StrRet5{};
StrRet1 = UKismetStringLibrary::Concat_StrStr(StrRet1, FString(TEXT(One)));
StrRet2 = UKismetStringLibrary::Concat_StrStr(StrRet2, FString(TEXT(__)));
StrRet3 = UKismetStringLibrary::Concat_StrStr(StrRet3, FString(TEXT(Two)));
StrRet4 = UKismetStringLibrary::Concat_StrStr(StrRet4, FString(TEXT(__)));
StrRet5 = UKismetStringLibrary::Concat_StrStr(StrRet5, FString(TEXT(Three)));
UKismetSystemLibrary::PrintString(this, StrRet4, true, true, FLinearColor(0.000000,0.660000,1.000000,1.000000), 2.000000);

The proper way would be to use + operator:

FString Str1 = "One";
FString Str2 = "Two";
FString Str3 = "Three";
FString Message = Str1 + "__" + Str2 + "__" + Str3;
//Without kismet onscreen printing can be done the following way. "GEngine" is a global variable. Note that this function doesn't print to console
GEngine->AddOnscreenDebugMessage(-1, 10, FColor::Cyan, Message);
2 Likes
FStringFormatOrderedArguments LogArguments;
LogArguments.Add(StringVariable1);
LogArguments.Add(StringVariable2);
LogArguments.Add(StringVariable3);
FString LogString = FString::Format(TEXT("%s %s %s"), LogArguments);
GEngine->AddOnScreenDebugMessage((uint64)-1, 5.f, FColor::White, *LogString);
3 Likes

What’s wrong with kismet library ?

1 Like

so its mean I can create a macro for this to make life easier?

FStringFormatOrderedArguments LogArguments;
LogArguments.Add(StringVariable1);
LogArguments.Add(StringVariable2);
LogArguments.Add(StringVariable3);

macro
#define LogString(Format, ...) if(GEngine){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, FString::LogString(TEXT(Format), ##__VA_ARGS__), false);}

call
LogString (“%s _ %s _ %s”, LogArguments);

kismet library is very cool, but I am learning pure C++ for UE4 and I just don’t want to use blueprint libraries incase if everything is slightly easy to do in pure C++, but yes sometimes I do use if things goes very complicated

1 Like

The thing about working with string is you want to avoid multiple allocations, I know you don’t like Kismet library but it is C++ at the end of the day and if you look at the definition here you can adjust it to multiple strings instead of just 2 and have it append all that in one allocation which should be faster than other methods.

FString UKismetStringLibrary::Concat_StrStr(const FString& A, const FString& B)
{
	// faster, preallocating method
	FString StringResult;
	StringResult.Empty(A.Len() + B.Len() + 1); // adding one for the string terminator
	StringResult += A;
	StringResult += B;

	return StringResult;
}
2 Likes

Very Informative , really appreciated :slight_smile: thank you so much for sharing this expensive information

1 Like

No the example I gave you is fine as is. Macros should be avoided for various reasons.

1 Like

I will need more time to learn to print strings without macros, they have hard syntax to remember