Another way to get Tank Mass without casting

In the lecture the way to retrieve the Tank Mass is:

	// Calculate and apply sideways force F = m a
	auto TankRoot = Cast<UStaticMeshComponent>(GetOwner()->GetRootComponent());
	auto CorrectionForce = (TankRoot.GetMass() * CorrectionAcceleration) / 2;	// Two tracks

	TankRoot()->AddForce(CorrectionForce);

The way I suggest is:

	// Calculate and apply sideways force F = m a
	auto TankMass = GetOwner()->GetRootPrimitiveComponent()->GetMass();
	auto CorrectionForce = (TankMass * CorrectionAcceleration) / 2;	// Two tracks

	GetOwner()->GetRootPrimitiveComponent()->AddForce(CorrectionForce);

Is the same thing… but maybe a little bit more clear :slight_smile:

Cheers

I correct myself…It was a BAD advice

Compiling with 4.16 I get the following warning message:

warning C4996: ‘AActor::GetRootPrimitiveComponent’: Use GetRootComponent() and cast manually if needed Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.

So forget it… The original way is the correct one :sweat_smile:

1 Like

Privacy & Terms