Difference between AddOnScreenDebugMessage and UE_LOG

If I add the following in my Character’s tick() function:



float jumpz = MyMovementComponent->JumpZVelocity;
UE_LOG(LogTemp, Warning, TEXT("JumpZVelocity: %s"), jumpz);


The game crashes with

If I then do it this way. it runs fine:



float jumpz = MyMovementComponent->JumpZVelocity;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::SanitizeFloat(jumpz));


As someone who is quite new to c++, and how it’s handling references to vars etc… what is going on?
I assume SanitizeFloat() is dereferencing the variable so I’m allowed to access it…?

I apologise for such a dumb question, just wasn’t sure what to google to understand this better (is it how UE4’s functions work or is it more of a basic c++ understanding)

Cheers

Logging | UE4 Community Wiki

I think that %s in the UE_LOG is expecting string as well, yet you are feeding it a float.
Have you simply tried:



UE_LOG(LogTemp, Warning, TEXT("JumpZVelocity: %s"), FString::SanitizeFloat(jumpz));


?

UE_LOG is a macro. Macros isn’t type safe and you need to use them carefully. %s means: replace it with c-string. No mather what you pass - it will be treated as a char array.
Float value is trated like null-terminated char array, so it tries to access subsequent bytes until it encounters null byte. You don’t know where that null byte is, so you end up with a memory access violation because you’re trying to get into memory that doesn’t belong to you = crash.

If you want display a float with UE_LOG use %f:



UE_LOG(LogTemp, Warning, TEXT("JumpZVelocity: %f"), jumpz);


Cheers ! This is very comprehesnive ! :smiley:

Ahh, that makes sense! My programming experience is mostly python and lua, but your explanation here helps me understand this a lot, thanks