Download

Help with loops

I think this is the right place to post it. Anyway i have been looking through the shooter example for an automatic fire system and its not very straight forward, would anyone be able to show me some example code or anything that would allow me to make it so when i hold my fire button down it constantly shoots projectiles and when i release it stops i can do this in blueprint using sequences and a gate but not sure how do it in c++

Here is an idea:

[h]

bool bCanFire;
bool bWantsToFire;
float CurrentFireTime;
float TargetFireTime;

[cpp]

Constructor::Constructor( )
{
    bCanFire = true;
    TargetFireTime = 0.15f;
}

Tick( float DeltaTime )
{
   Super::Tick( DeltaTime );
   if( !bCanFire )
   {
      CurrentFireTime += DeltaTime;
      if( CurrentFireTime >= TargetFireTime )
      {
          bCanFire = true;
          CurrentFireTime = 0.f;
      }
   }

    if( bWantsToFire && bCanFire )
    {
        Fire( );
    }
}

// Input to set fire
InputWantToFire( )
{
    // Flip want to fire each call ( first press = true, second press = false, continued... )
    bWantToFire = !bWantToFire;
}

Fire( )
{
    // Fire stuff (spawn projectile)

    bCanFire = false;
}

I’m not sure what i am doing wrong this is what i have got so far Screenshot - 2baf28d38a046cb1c1bfc95c6e269923 - Gyazo Screenshot - 4e6be138f91beecf7e42a707235edf2f - Gyazo i am missing the “InputWantToFire()” not sure how i would do it would i just do this? it compiles but it still lets me shoot 1 shot at a time not when i hold the button down it constantly shoots and when i let go it stops

void ACodeLearningProjectCharacter::InputWantToFire()
{
bWantToFire = !bWantToFire;
}

You could try adding:

[h]

UFUNCTION( )
void InputWantToFire( );

.cpp]

void ACodeLearningProjectCharacter::InputWantToFire( )
{
    bWantToFire = !bWantToFire;
}

Then, change:

PlayerInputComponent->BindAction( "Fire", IE_Pressed, this &ACodeLearningProjectCharacter::OnFire );

To:


PlayerInputComponent->BindAction( "Fire", IE_Pressed, this &ACodeLearningProjectCharacter::InputWantToFire);
PlayerInputComponent->BindAction( "Fire", IE_Released, this &ACodeLearningProjectCharacter::InputWantToFire);

Thank you both for the help got it working now and hopefully i can learn from this:)