Firing Status Starts as "Reloading", but Never Returns to it After Firing

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?

1 Like

Please use code formatting

```cpp
code goes here
```

Or highlight the code and use the </> button.


Have you stepped through the code with the debugger or even just adding logs, e.g. in all control paths for the tick function?

I did log out each firing status within the Tick component. Once the initial firing state changed from reloading, reloading never appeared again.

I’ve since found the issue. In setting up to prevent AI tanks from firing sooner than the player tank, I cut the firing state setting code from the Fire method and moved it to Being Play. Copying it and putting it in the Fire method solved the problem as the Firing State now changes to Reloading whenever Fire is called.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms