Logging values gets me this error

Here is an example of how to log an int:

UE_LOG(YourLog,Warning,TEXT(“MyCharacter’s Health is %d”), MyCharacter->Health );

Use %d instead of %s and don’t use a dereference *.

I’m trying to log a string then an int value and it’s just not working. I have looked up answers but nothing is helping me at all so can someone please put my question to rest. I just want to log a string + int to screen. This is what I have so far.

		if (GEngine) {
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Number in the world: %s"), *iNumberOfBasicUnits);
		}

All that is logged is this

Number in the world: %s

I’m also getting an error message:

operand of '*' must be a pointer

You’re trying to print an int with string formatting it seems.

Try this instead:

         if (GEngine) {
             GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Number in the world: %d"), iNumberOfBasicUnits);
         }

However I’d recommend using UE_LOG instead, but it won’t print to screen, it’ll print into your output log, like so:

         if (GEngine) {
            UE_LOG(LogTemp, Warning, TEXT("Number in the world: %d"), iNumberOfBasicUnits);
         }

Reference this documentation:

That doesn’t work. it prints
Number in the world: %d

Can you post your source code please?

iNumberOfBasicUnits = aBasicUnit.Num();
if ((iMaximumOfBasicUnit - iNumberOfBasicUnits <= 10) && bCanSpawnBasicUnits == true) {

		//spawns more units without setting timer
		spawnMoreAi(EAiToSpawn::BasicUnit, iMaximumOfBasicUnit - iNumberOfBasicUnits);

		if (GEngine) {
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("number of basic units: %d"), iNumberOfBasicUnits);
		}
	}

Try this:

if (GEngine) {
              GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf("Number in the world: %d"), iNumberOfBasicUnits);
          }

GEngine works a bit differently when you’re adding in values. I believe this code will work. I’m used to using UE_Log and I’m making the assumption they work the same…which I don’t think is the case. In fact it says to use Fstring::Printf right in the documentation I posted when adding values to your string.

i tried it with UE_LOG and it worked so i will just do that.