Throttle logic question - redundant code?

Hi – I’m confused about one area of the code in the section.

In the example you’ve placed the DriveTrack and ApplySidewaysForce into the OnHit function – which means that it will run every frame that the tracks are hitting the ground. But you also reset CurrentThrottle to 0 in that method ie

void UTankTrack::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit) {
DriveTrack();
ApplySidewaysForce();
CurrentThrottle = 0;
}

So – my question is – why do we need to add CurrentThrottle to itself in the SetThrottle method? Won’t CurrentThrottle always be 0 to start with here?

CurrentThrottle = FMath::Clamp(CurrentThrottle + Throttle,-1,1);

My question being wouldn’t this work the same:

CurrentThrottle = FMath::Clamp(Throttle,-1,1);

1 Like

because we have CurrentThrottle = CurrentThrottle + Throttle, which sometimes became more than 1
thats why we need to reset CurrentThrottle = 0; and to Clamp() it

I am having the Same thought here. If you try this CurrentThrottle = FMath::Clamp(Throttle,-1,1); the tank wont be able to move except forward for some reasons. It is a mysterius bug

I think I know why. My tank has some problem moving and while debugging I also came across this code change, which made things worse.
Because we have four events in the Tank_BP calling the SetThrottle() function (IntendMoveForward and IntendMoveRight, which call the TankMovementComponent and then call the SetThrottle() function with the ‘Throw’. The other two I left in from the conventional handling (vs. fly-by-wire) which are ThrottleLeftTrack and ThrottleRightTrack, they are calling the SetThrottle() function directly with the axis value.)

These four events (or two events in the case you deleted the conventional handling as done during the course) are calling the SetThrottle() function kind of every frame with an input between -1 and +1. If that event doesn’t have any input (e.g. only intending to move forward or only intending to turn) then the event will still call the SetThrottle() function but will pass on a value of 0 and therefore setting the Member variable CurrentThrottle to zero.

So with
CurrentThrottle = FMath::Clamp(Throttle,-1,1);
it can happen that the CurrentThrottle is set to , lets say, +1, but before calling DriveTrack() in the OnHit() function, the CurrentThrottle is already set to 0 again by another function calling SetThrottle() the same frame.

Using
CurrentThrottle = FMath::Clamp(CurrentThrottle + Throttle,-1,1);
will not replace CurrentThrottle, but add to it. So any value that ever has been assigned to it, will stay and add up. Calling the SetThrottle function with a value of zero will not erase it, but just not change the current exisiting value of CurrenThrottle, until it is reset by
CurrentThrottle = 0;
AFTER calling the DriveTrack() function.

Privacy & Terms