Bool bHaveAimSolution Confusion

Hello Forum,

I’ve made it a habit to read through my C++ after each video to make sure I understand how the code is functioning. This time I’m come upon a line that I don’t understand.

From: TankAimingComponent.cpp
bool bHaveAimSolution = UGameplayStatics::SuggestProjectileVelocity
(
this,
OutLaunchVelocity,
StartLocation,
HitLocation,
LaunchSpeed,
ESuggestProjVelocityTraceOption::DoNotTrace
);

The SuggestProjectileVelocity method obviously takes in and returns a great amount of data. So what are we saying here by including it in our bool? A boolean is true or false, so what value from SuggestProjectileVelocity is determining whether bHaveAimSolution is true or false?

Next, we use an if statement.

if (bHaveAimSolution)

It’s my understanding that this is the same as writing if (bHaveAimSolution = true) but again, what is determining whether our bool is true or false? Would it ever be false?

Any answers would be appreciated.

Thank you.

The return value from the function.

cough == cough

bool Example(int a, int b, int c)
{
    bool bFoundAValidSolution = false;
    //do calculations
    return bFoundAValidSolution;
}

int main()
{
    bool bHaveAimSolution = Example(1,2,3);
}

The code itself should be fairly self-explanatory; the end result is to find out whether there is an aiming solution (i.e. the function can figure out an arc trajectory, given point A, point B, and a speed), hence the name “HaveAimSolution”. The function SuggestProjectileVelocity() returns that boolean value and only that alone. Everything else works as an out parameter.

Recall that an out parameter is when a function changes the value of something else in memory. The SuggestProjectileVelocity() method only returns whether it has an aiming solution, but it also changes an out parameter to the suggested projectile velocity. To find out which arguments passed to a function are out parameters, you can look at the function’s code in GameplayStatics.cpp; I just use Visual Studio’s “Go To Definition” option. The engine code seems to always label out parameters with “Out” prepended to the variable name (a practice I’ve adopted to my own code that I’d recommend); in this case it is FVector& OutTossVelocity.

In this case, aside from whether there is an aiming solution, we are also interested in the the suggested projectile velocity. I found this a bit confusing at first, but found that it’s only suggesting the direction of the trajectory, not the speed. It simply multiplies the direction of the projectile launch by the speed you provided as one of the other arguments to the function. From there, we deduce the direction of the velocity it gives us (the unit vector; the velocity / speed so we have just the direction without the magnitude of the vector) and use that to set the direction of the barrel and turret.

Here’s the full method signature that I ended up using, instead of the abbreviated one in the lecture.

bool bHaveAimingSolution = UGameplayStatics::SuggestProjectileVelocity( this, OutLaunchVelocity, StartLocation, HitLocation, LaunchSpeed, false, 0.0f, 0.0f, ESuggestProjVelocityTraceOption::DoNotTrace, FCollisionResponseParams::DefaultResponseParam, TArray(), false );

You can change the last parameter to true in order to enable DrawDebug, which will let you visualize the projectile arc that the function is suggesting. Keep in mind this suggestion is very much separated from the actual path the projectile will take, as the projectile will be tied to the direction the barrel is actually pointed in. (In which case, we could improve the accuracy of this by changing the starting point from the Muzzle socket to the starting point of something else, such as the turret, but I digress.)

Hopefully this makes sense, but I probably over-explained everything since I found some of it rather confusing at first. If any of this explanation is incorrect, I welcome others to elaborate on that and would appreciate it.

Privacy & Terms