Using Unreal 4.21.2 on Windows 10, Visual Studio 2017 Community
I’ve followed Ben’s coding and the reticle begins red, turns amber, then green when the game starts. Then the AI tanks fire a steady stream of projectiles. I’ve logged FiringState and see that Reloading appears only at begin play. Here is the code snip:
void UTankAimingComponent::BeginPlay()
{
// so that first fire is after initial reload
LastFireTime = FPlatformTime::Seconds();
}
// Setter method
void UTankAimingComponent::Initialise(UTankBarrel* BarrelToSet, UTankTurret* TurretToSet)
{
Barrel = BarrelToSet;
Turret = TurretToSet;
}
void UTankAimingComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
if ((FPlatformTime::Seconds() - LastFireTime) < ReloadTimeInSeconds)
{
FiringState = EFiringState::Reloading;
UE_LOG(LogTemp, Warning, TEXT(“Reloading”))
}
else if (IsBarrelMoving())
{
FiringState = EFiringState::Aiming;
}
else
{
FiringState = EFiringState::Locked;
}
}
Firing code is:
void UTankAimingComponent::Fire()
{
if (FiringState != EFiringState::Reloading )
{
// Spawn a projectile at the socket location on the barrel
if (!ensure(Barrel)) { return; }
if (!ensure(ProjectileBluePrint)) { return; }
auto Projectile = GetWorld()->SpawnActor(
ProjectileBluePrint,
Barrel->GetSocketLocation(FName(“Projectile”)),
Barrel->GetSocketRotation(FName(“Projectile”))
);
Projectile->LaunchProjectile(LaunchSpeed);
}
}
I’ve gone through the lecture twice reverting to code state prior to this lecture and can’t figure out why FiringState never changes to “Reloading” after the begin play. If I switch the chevron in the time comparison line, the status stays “Reloading”. Does anyone see anything in the code?
On another note, every time I recompile my code, the Tank Blueprint loses the Projectile from the Aiming Component. Any ideas?