Wrong C++ int64 return

C++ function never return int64. Works like with int32. As example,
.h

UFUNCTION(BlueprintCallable, meta = (DisplayName = “TestFunc”))
static int64 TestFunc();

.cpp

int64 TestFunc(){
return 2147483647+2;
}

returns -2147483647

I cant fix it, help, please.

The issue is that you are doing the operation with two int32 and then converting to int64: StaticCast<int64>( (int32 + int32) ). You need to do the operation between an int32 against int64 so the number doesnt overflow.

For example:

int64 i = MAX_int32;
i += 2;
return i; // 2147483649
// 
return StaticCast<int64>(MAX_int32) + 2; // 2147483649
// ..
return MAX_int32 + StaticCast<int64>(0) + 2; // 2147483649
// ..
return MAX_int32 + 2 + StaticCast<int64>(0); // -2147483647
// ..
return StaticCast<int64>(MAX_int32 + 2); // -2147483647
// ..
UE_LOG(LogTemp, Warning, TEXT("int64: %lld"), TestFunc());

Hope it helps.

3 Likes

Ok, thank you. It really works for C++, but still not works when i’m trying return result to Blueprints. Maybe you know how to solve this problem?


EDIT: We have to convert from int64 → text → string because the the only function in KismetTextLibrary.h that takes int64 as parameter is static FText Conv_Int64ToText(...).

3 Likes

Wow, it works. Thank you too much.

1 Like

Thanks for this.
Do you happen no know why this produces values like this, it seems like if it’s above max in32 it doesn’t work and it returns -2147483648

Edit: IDK… lol

It should work at first glance.

There’s an overloaded 64 version and I think it’s calling it but it also has the same issue, I tested in C++, I changed TruncToInt with TruncToInt64 and it worked, I think it’s a bug.

1 Like

Should’ve dedicate a bit more time digging. My mistake.

Oh you’re fine, you provided the correct location of where the issue might be. I think I’ll submit a bug report unless someone else provides a good explanation of this behavior.

1 Like