String formatting problem

Hello! I am pretty new to Unreal Engine 4 ( I recently came to your realm from the kingdom of Unity and I must say that I am not dissapointed) and I have a problem. Im not sure if this is the right category for this kind of topic but I will give it a shoot.

void AMyActor::CalculateProperties(){
	FString debugText = FString::Printf(TEXT("Total damage: %s | Damage time: %s | Damage per second: %s"), FString::FromInt(totalDamage), FString::SanitizeFloat(damagePerSecond), FString::FromInt(damagePerSecond));
	damagePerSecond = totalDamage / damageTime;
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, debugText);

So, basically, on the second line I get an error and I am not sure why. Could you please help me?

What’s the error?

Info Total build time: 7.63 seconds
Info Compiling game modules for hot reload
Info Performing 2 actions (4 in parallel)
Info MyActor.cpp
Error C:\Program Files\Epic Games\4.8\Engine\Source\Runtime\Core\Public\Containers\UnrealString.h(1163) : error C2665: ‘CheckVA’ : none of the 11 overloads could convert all the argument types
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(98) : could be ‘bool CheckVA(bool)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(97) : or ‘void *CheckVA(ANSICHAR *)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(96) : or ‘TCHAR CheckVA(TCHAR)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(95) : or ‘long CheckVA(unsigned long)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(94) : or ‘long CheckVA(long)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(93) : or ‘double CheckVA(double)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(92) : or ‘int64 CheckVA(int64)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(91) : or ‘uint64 CheckVA(uint64)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(90) : or ‘int32 CheckVA(int32)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(89) : or ‘uint8 CheckVA(uint8)’
Info c:\program files\epic games\4.8\engine\source\runtime\core\public\Misc/OutputDevice.h(88) : or ‘uint32 CheckVA(uint32)’
Info while trying to match the argument list ‘(FString)’
Info C:\Users\Documents\Unreal Projects\BasicTraining\Source\BasicTraining\MyActor.cpp(39) : see reference to function template instantiation ‘FString FString::Printf<FString,FString,FString>(const TCHAR *,T1,T2,T3)’ being compiled
Info with
Info T1=FString
Info , T2=FString
Info , T3=FString
Info ]
Info -------- End Detailed Actions Stats -----------------------------------------------------------
Info ERROR: UBT ERROR: Failed to produce item: C:\Users\Documents\Unreal Projects\BasicTraining\Binaries\Win64\UE4Editor-BasicTraining-6116.dll

Try dereferencing the FString. (It will return TCHAR).

*UE_LOG(LogActor, Error, TEXT("%s"), MyFString); works the same way.

So it should work:

void AMyActor::CalculateProperties()
	FString debugText = FString::Printf(TEXT("Total damage: %s | Damage time: %s | Damage per second: %s"), *FString::FromInt(totalDamage), *FString::SanitizeFloat(damagePerSecond), *FString::FromInt(damagePerSecond));
	damagePerSecond = totalDamage / damageTime;
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, debugText);

1 Like

Yep, it worked. Thank you very much.

I am still curious about one thing. What does the “*” symbol does before the FString in FString::Printf ?

The “*” character is a dereference operator in that context.

For more information about FString have a look at the documentation:

1 Like

Thank you very much. :smiley:

* in front of FString::Print is not a dereferencing operator but an overloading operator. It is one of the odd features provided by Unreal Engine. :slight_smile: