Why does calculation within FVector2D declaration not work?

It took my way too long to figure out where my code is not doing what it should be doing.
I don’t understand why this doesn’t work. Especially, because I saw more or less the same code in a tutorial.
So here is an example:

const FVector2D CurrentLocation1 = FVector2D((4 + 1) / 10, (4 + 1) / 10);
UE_LOG(LogTemp, Warning, TEXT("CurrentLocation1: %s"), *CurrentLocation.ToString());
const FVector2D CurrentLocation2 = FVector2D(0.5, 0.5);
UE_LOG(LogTemp, Warning, TEXT("CurrentLocation2: %s"), *CurrentLocation.ToString());

The log says

CurrentLocation1: X=0.000 Y=0.000
CurrentLocation2: X=0.500 Y=0.500

Why is that? Am I missing something here? Why can’t I do basic calculations in the declaration of the FVector2D while in the sample project I got this from they are doing all kinds of calculations in that line?

Edit: It seems like the problem lies in the division because (4+1) logs as 5 while (4+1)/10 logs as 0. But I still don’t understand why divisions shouldn’t be possible.

Because you’re using integers in the first example rather than floats. The difference is very important since integer division does not produce floating-point values.

In this case the integer operations will be performed first. They will then be implicitly casted to floats when initializing the FVector2D.

1 Like

You’re right. Thank you!
Someone on the subreddit already pointed that out to me. I haven’t had the chance to test yet, but it’s pretty safe to say that that’s the problem.
I kind of assumed that (4+1)/10 would automatically result in a float. I guess I’m spoiled by blueprint.

1 Like