Updating TArray of Projectiles when Projectile Destroyed

To give a brief rundown of this issue:

In my weapon class I declare a TArray of projectiles.

TArray<AUTProjectile*> CurrentCount;

This allows me to iterate through all of my projectiles and blow them up simultaneously, find the count of projectiles, etc.

My projectiles will blow up if they take any damage though. Based on my logging it looks like after my projectile is destroyed my CurrentCount array is not updated, so I need to update CurrentCount to remove the pointer to my projectile before it’s destroyed. What I’m doing is this.

float AUTProj_Grenade::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser)
{
    if (EventInstigator != NULL)
    {
        AUTCharacter* UTP = Cast<AUTCharacter>(EventInstigator->GetPawn());
        AUTCharacter* UTOwner = Cast<AUTCharacter>(Instigator);
        if (UTP != NULL && UTOwner != NULL && (UTP->GetTeamNum() == 255 || UTP->GetTeamNum() != UTOwner->GetTeamNum()))
        {
            OwningGrenadeLauncher->ClearGrenade(this);
            Explode(GetActorLocation(), FVector::UpVector);
        }
    }

    return Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
}

The ClearGrenade function is currently defined as so:

void AUTWeap_GrenadeLauncher::ClearGrenade(const AUTProj_Grenade& ClearedGrenade);
{
    for (int32 i = 0; i < CurrentCount.Num(); i++)
    {
        if (ClearedGrenade == *CurrentCount[i])
        {
            CurrentCount[i]->ShutDown();
            CurrentCount.RemoveAt(i);
        }
    }
}

At the moment I think this is conceptually sound, but my C++ syntax here is getting me some compilation errors. I’m not really sure what to make of this since it’s seemingly to do with my function declaration.

1>UnrealTournament\Public\UTWeap_GrenadeLauncher.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
UnrealTournament\UnrealTournament\Source\UnrealTournament\Public\UTWeap_GrenadeLauncher.h(36) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

I could probably handle this by keeping a separate int32 value for CurrentCount and refactoring ClearedGrenade to simply decrement this int32 value, rather than using CurrentCount.Num() to return the number of projectiles I have active. That being said, I’d prefer to handle it this way so I am not duplicating unnecessary information. Anyone able to point me in the right direction here?

Which function declaration is wrong? Line 36 tells us nothing :slight_smile:

Compilation error is complaining on this line in Weapon.h

virtual void ClearGrenade(const AUTProj_Grenade& ClearedGrenade);

Corresponding implementation in Weapon.cpp

void AUTWeap_GrenadeLauncher::ClearGrenade(const AUTProj_Grenade& ClearedGrenade)

Getting an AActor by reference is very unusual way to do things, try this

virtual void ClearGrenade(const AUTProj_Grenade* ClearedGrenade);

Corresponding implementation in Weapon.cpp

 void AUTWeap_GrenadeLauncher::ClearGrenade(const AUTProj_Grenade* ClearedGrenade)

Also you shouldnt keep iterating over an array after removing an entry like this

 for (int32 i = 0; i < CurrentCount.Num(); i++)
     {
         if (ClearedGrenade == *CurrentCount[i])
         {
             CurrentCount[i]->ShutDown();
             CurrentCount.RemoveAt(i);
         }
     }

you will end up skipping entries!

you need to either restart

i = 0;

or go back 1

i--;

Hey Rama,

Thanks for your help and your suggestions here. I ended up taking a step back on my implementation and rebuilding from the ground up, keeping in mind your suggestions. It turns out some of the major issues I was having were due to my oversight in failing to make forward declarations (Doh!).

I’m still having some issues to work out, but these are unrelated to the compilation issues I was having or removing items from arrays, so I appreciate the help!