How can I print an onscreen bool value?

Been trying to get a bool value to print to the screen.

GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Blocking Hit =: %s"), bBlockingHit));

How can I convert a bool to something legibale? printing true or false.

1 Like

You don’t really need function, try this

bBlockingHit ? "True" : "False"

I used like this:

bool MyBoolValue = true;
UE_LOG(MyGame, Log, TEXT("Bool value is: %s"), MyBoolValue? "true" : "false" );

and in Log i got this:

Bool value is: ??

How to solve this problem?

Hey, just saw your question. I don’t know if you still have the problem, but if someone else runs into this problem, try doing it like this:

if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Black, FString::Printf(TEXT("Bool: %s"), bMyTestBool ? TEXT("true") : TEXT("false")));
	}

Also for you UE_LOG print, try adding

TEXT("true") : TEXT("false")

instead of only “true” and “false”

11 Likes

Smooth :slight_smile:

I like this:)

#include "Kismet/KismetStringLibrary.h"

if (GEngine)
    GEngine->AddOnScreenDebugMessage(-1, INFINITE, FColor::Red, 
UKismetStringLibrary::Conv_BoolToString(bBlockingHit));

Tested on UE 4.10.1

https://github.com/EpicGames/UnrealEngine/blob/a27ad66f0a075f3b74ef8f68a4b2b0da4882425e/Engine/Source/Runtime/Engine/Private/KismetStringLibrary.cpp#L65

FString UKismetStringLibrary::Conv_BoolToString(bool InBool)
{
	return InBool ? TEXT("true") : TEXT("false");	
}

So really does not matter. That function btw is a node for blueprint and it’s not used outside of it (at least GitHub don’t find any use of that function)

Yes, thank you, I know I do and found it in Source, I’m just lazy :slight_smile:

I’m a little late, but you can create macro like this

#define BToS(b) b ? L"true" : L"false"

Or more correct

#define BToS(b) b ? TEXT("true") : TEXT("false")

So it will be just

GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Blocking Hit =: %s"), BToS(bBlockingHit)));
2 Likes

How are you in every forum post I look at

A simpler way that doesn’t use macros and defines:

		bool myBool = true;

		if (GEngine)
		{
			const FString msg = FString::Printf(TEXT("Bool Status: %d"), myBool);
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, *msg);

			// or
			const FString msg2 = FString::Printf(TEXT("Bool Status: %s"), myBool ? TEXT("TRUE") : TEXT("FALSE"));
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, *msg2);
		}
1 Like