[BUG] Function return bad bool by reference

Hello!
I’m using newest vs2017 and use UE 4.25.3 and Development Editor.

I found very frustrating bug. Simple C++ Function ClampVector which return clamped vector and bool informing if is clamped or not. Function work well, but bool return bad value.

If have few “if”, and every setting of “bool IsClamped = true;” inside “if {…}” is optimized and completly not set variable !!!
if I write “IsClamped = true;” outside “if {…}” then it works well, and function return good value.

I try to use:
PRAGMA_DISABLE_OPTIMIZATION
function…
PRAGMA_ENABLE_OPTIMIZATION

but it doesn’t help.

Why I know it is a bug? Well, when I debug inside blueprint which stop on my “Branch” node after this function which condition is returned “IsClamped” - then it suddenly show good returning value and with this brakepoint resume frame by frame, my game works well !!!
Same situation if I set brakepoint inside Visual Studio. With brakepoint suddenly function start returning good value !!!

Also funny thing. When I put this line just before function exit:
UE_LOG(LogTemp, Warning, TEXT(“ClampVector = %i”), IsClampedL);
Log show me good value - but function return bad value :slight_smile: :slight_smile: :-).

I also tried:

  1. return value by reference
  2. return value by function return.

In all cases I have problem, my bool iside function work bad !

I read some posts from few years, but non of them solve my problem. For example:

don’t help my.

Ahh, another information, my function is insinde Blueprint Function Library in C++.

Here is my function code in version of return value by function “return”:

PRAGMA_DISABLE_OPTIMIZATION
bool UWABBlueprintFunctionLibrary::ClampVector (FVector Value, FVector Min, FVector Max, FVector & RetValue)
{
	bool IsClampedL = false;

	RetValue = Value;

	/// X
	if (Min.X < Max.X)
	{

		if (Value.X < Min.X) {
			IsClampedL = true;
			RetValue.X = Min.X;
		}
		else {
			if (Value.X > Max.X) {
				IsClampedL = true;
				RetValue.X = Max.X;
			}
			else {
				RetValue.X;
			};
		};

	} else {

		if (Value.X < Max.X) {
			IsClampedL = true;
			RetValue.X = Max.X;
		}
		else {
			if (Value.X > Min.X) {
				IsClampedL = true;
				RetValue.X = Min.X;
			}
			else {
				RetValue.X;
			};
		};

	};

	/// Y
	if (Min.Y < Max.Y)
	{

		if (Value.Y < Min.Y) {
			IsClampedL = true;
			RetValue.Y = Min.Y;
		}
		else {
			if (Value.Y > Max.Y) {
				IsClampedL = true;
				RetValue.Y = Max.Y;
			}
			else {
				RetValue.Y;
			};
		};

	} else {

		if (Value.Y < Max.Y) {
			IsClampedL = true;
			RetValue.Y = Max.Y;
		}
		else {
			if (Value.Y > Min.Y) {
				IsClampedL = true;
				RetValue.Y = Min.Y;
			}
			else {
				RetValue.Y;
			};
		};

	};

	/// Z
	if (Min.Z < Max.Z)
	{

		if (Value.Z < Min.Z) {
			IsClampedL = true;
			RetValue.Z = Min.Z;
		}
		else {
			if (Value.Z > Max.Z) {
				IsClampedL = true;
				RetValue.Z = Max.Z;
			}
			else {
				RetValue.Z;
			};
		};

	} else {

		if (Value.Z < Max.Z) {
			IsClampedL = true;
			RetValue.Z = Max.Z;
		}
		else {
			if (Value.Z > Min.Z) {
				IsClampedL = true;
				RetValue.Z = Min.Z;
			}
			else {
				RetValue.Z;
			};
		};

	};

	UE_LOG(LogTemp, Warning, TEXT("ClampVector = %i"), IsClampedL);

	return IsClampedL;

}
PRAGMA_ENABLE_OPTIMIZATION