Possible solution with less side-effects - Feedback Please

Was working through the challenge in this lecture and came to a slightly different solution then what Sam ended up with. It has fewer side-effects and feel it is a little easier to reconcile with than Sam’s solution and want to see if there are any cons to my solution.

I added the following private variable in the header:

bool bReverseDirection;

And my tick method is as follows:

void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

if (HasAuthority())
{
	FVector FromLocation = bReverseDirection ? GlobalTargetLocation : GlobalStartLocation;
	FVector ToLocation = bReverseDirection ? GlobalStartLocation : GlobalTargetLocation;

	FVector Location = GetActorLocation();
	float JourneyLength = (ToLocation - FromLocation).Size();
	float JourneyTravelled = (Location - FromLocation).Size();

	if (JourneyTravelled >= JourneyLength)
	{
		bReverseDirection = !bReverseDirection;
	}
	
	FVector Direction = (ToLocation - FromLocation).GetSafeNormal();
	Location += Speed * DeltaTime * Direction;
	SetActorLocation(Location);
}
}

This solution only changes the bReverseDirection variable and none of the GlobalStartLocation and GlobalTargetLocation which makes the keeps the state of the object less confusing.

How do others feel about this? Are there any cons to this solution?

Privacy & Terms