Explaining Barrel Code

Hello Forum,

I’ve been struggling with understanding how all of the methods in this lecture relate to on another. I’m going to explain it the way I understand it and if anyone sees a flaw in my notes, please let me know.

From: TankAimingComponent.h
UStaticMeshComponent* Barrel = nullptr;

In the header for TankAimingComponent, we create a pointer to a UStaticMeshComponent (that is an element of our mesh) and instantiate it to a null pointer.

From: Tank.h
UFUNCTION(BlueprintCallable, Category = Setup)
void SetBarrelReference(UStaticMeshComponent* BarrelToSet);

Here in our header file, we declare a method named “SetBarrelReference”. This method will receive a pointer to a static mesh as its parameter. It will label that parameter “BarrelToSet”.

These nodes from the Tank_BP blueprint will execute SetBarrelReference() when the game begins. This will provide SetBarrelReference() with an argument. That argument is the barrel component of our tank pawn.

From: Tank.cpp
void ATank::SetBarrelReference(UStaticMeshComponent* BarrelToSet)
{
TankAimingComponent->SetBarrelReference(BarrelToSet);
}

The nodes in the Tank_BP event graph tell this method to execute when the game begins. This method runs the SetBarrelReference() method inside of TankAimingComponent.cpp and passes it the value of whatever is wired into the “Barrel to Set” pin of the “Set Barrel Reference” node in our blueprint.

From: TankAimingComponent.h
void SetBarrelReference(UStaticMeshComponent* BarrelToSet);

Here we declare a function with the same name as we created in Tank.h. This function also receives a pointer to a static mesh which it gives the handle, “BarrelToSet”.

From: TankAimingComponent.cpp
void UTankAimingComponent::SetBarrelReference(UStaticMeshComponent* BarrelToSet)
{
Barrel = BarrelToSet;
}

This function is called from inside SetBarrelReference() in Tank.cpp which is executed by the blueprint when the game begins. It sets the barrel variable in TankAimingComponent.h to be equal to the barrel we have wired into the blueprint which has been given the handle, “BarrelToSet”.

We’ve finished finding and setting the barrel of our tank. Let’s take a look at how that value is used.

From: TankAimingComponent.cpp
void UTankAimingComponent::AimAt(FVector HitLocation)
{
auto OurTankName = GetOwner()->GetName();
auto BarrelLocation = Barrel->GetComponentLocation().ToString();
UE_LOG(LogTemp, Warning, TEXT("%s aiming at %s from %s"), *OurTankName, *HitLocation.ToString(), *BarrelLocation);
}

Whenever AimAt() is executed, BarrelLocation will be printed out by the UE_LOG macro. Thanks to our work on setting the Barrel variable, BarrelLocation will be equal to the the barrel component we wired into our Tank_BP blueprint. Since “auto” knows that BarrelLocation is a vector and ToString() converts this into printable text, our Output Log in Unreal will read out the location in the game world of our barrel. Since TankPlayerController.cpp runs AimAt() inside of its Tick() function, this value will be printed on every frame.

Please let me know if this breakdown is correct. And thanks for reading.

Cheers.

2 Likes

Pretty much.

1 Like

Wow. Thank you for this breakdown. I also was a bit befuddled after the lesson, and this really helped me digest everything.

Privacy & Terms