Code running weirdly, jumping everywhere

Hello everyone, I have a bit of a problem…

I recently started coding in UE4 and I saw some weird behaviour.
I’m trying to get tracing to work, so to get to the point:
In the Visual Studio Debugger I put a breakpoint before an if statement, but the code ran even when the parameter was false, then it proceeded to jump in and out of the statement.

void UVehicle_MovementComp::Trace()
{
	for (int i = 0; i < tracerPoints.Num(); i++)
	{
		//gets weird here:
		if (tracerPoints[i]->tag.Compare(FString(TEXT("Predictable"))) == 0)
		{
			bool hit = World->LineTraceSingleByObjectType(
			traceHits[i], 
			tracerPoints[i]->startPoint, 
			tracerPoints[i]->endPoint, 
			FCollisionObjectQueryParams::AllObjects, 
			traceParams);
			//in this instance this is false, 
			//but it goes inside anyway
			if (hit)
			{
				//this is even weirder:
				//this runs, then jumps out,
				//then it runs again,
				//then it jumps inside the 
				//outer else branch
				FString str = 
					TEXT("ID:") + 
					FString::FromInt(i) + 
					TEXT(" - RayTrace Hit! ") + 
					FString::SanitizeFloat(traceHits[i].Distance);
				//this never runs, as it shouldn't,
				//because it shouldn't even get this far
				if (IsValid(traceHits[i].GetActor()))
					str.Append(
						TEXT(" (") + 
						traceHits[i].GetActor()->GetName() + 
						TEXT(")"));
				if (IsValid(traceHits[i].GetComponent()))
					str.Append(
						TEXT(" (") + 
						traceHits[i].GetComponent()->GetName() + 
						TEXT(")"));
				GEngine->AddOnScreenDebugMessage(
					-1, 
					0.015f, 
					FColor::Green, 
					str);
			}
			//this never runs:
			else
			{
				traceHits[i].Reset(1, false);
			}
		}
		else
		{
			//it continues here, runs this line,
			//freaks out, then jumps to the end
			//(No breaks or warnings)
			bool hit = World->LineTraceSingleByObjectType(
			traceHits[i], 
			tracerPoints[i]->startPoint, 
			tracerPoints[i]->endPoint, 
			FCollisionObjectQueryParams::AllObjects, 
			traceParams);
			if (hit)
			{
				FString str = 
					TEXT("ID:") + 
					FString::FromInt(i) + 
					TEXT(" - RayTrace Hit! ") + 
					FString::SanitizeFloat(traceHits[i].Distance);
				if (IsValid(traceHits[i].GetActor()))
					str.Append(
						TEXT(" (") + 
						traceHits[i].GetActor()->GetName() + 
						TEXT(")"));
				if (IsValid(traceHits[i].GetComponent()))
					str.Append(
						TEXT(" (") + 
						traceHits[i].GetComponent()->GetName() + 
						TEXT(")"));
				GEngine->AddOnScreenDebugMessage(
					-1, 
					0.015f, 
					FColor::Yellow, 
					str);
			}
			else
			{
				traceHits[i].Reset(1, false);
			}
		}
	}
}

If you need any more info, I’ll gladly help.
Thanks

Edit:Here’s part of the header file:

struct TracePoints{
	FVector startPoint;
	FVector endPoint;
	FString tag;

	TracePoints(FVector start = FVector::ZeroVector, 
	FVector end = FVector::ZeroVector, 
	FString tag = TEXT("")) :
		startPoint(start), endPoint(end), tag(tag) {}
};

UCLASS()
class AEROX_API UVehicle_MovementComp : public UPawnMovementComponent
{
	GENERATED_BODY()
	
private:

	TArray<AActor*> tracerIgnoredObjects;

	TArray<TracePoints*> tracerPoints;

	FCollisionQueryParams traceParams = FCollisionQueryParams(true);

	TArray<FHitResult> traceHits;

	USkeletalMeshComponent* parentMesh;

	void Trace();

protected:
	//Init
	virtual void InitializeComponent() override;

	//Tick
	virtual void TickComponent(
	float DeltaTime, 
	enum ELevelTick TickType, 
	FActorComponentTickFunction *ThisTickFunction) override;

}

Are you building in debug? If not, the compiler has probably rearranged code on you for optimization reasons. You can’t really trust single stepping through optimized code.

That was indeed the problem! Thank you for the help :slight_smile: Now to investigate why the trace returns false, while there is a cube it’s hitting…