UPROPERTY(EditDefaultsOnly, Category = EnemySpawn)
TSubclassOf<class AEnemy> EnemySpawn;
void AQuests::SpawnEnemies()
{
Spawner.SetNum(NumberRemaining);
for (int32 i = 0; i < Spawner.Num(); i++)
{
Spawner* = GetWorld()->SpawnActor<AEnemy>(EnemySpawn);
}
}
}
void AWeapon::ProcessInstantHit(const FHitResult &Impact, const FVector &Origin, const FVector &ShootDir, int32 RandomSeed, float ReticleSpread)
{
const FVector EndTrace = Origin + ShootDir * WeaponConfig.WeaponRange;
const FVector EndPoint = Impact.GetActor() ? Impact.ImpactPoint : EndTrace;
DrawDebugLine(this->GetWorld(), Origin, Impact.TraceEnd, FColor::Black, true, 10000, 10.0f);
AEnemy* Enemy = Cast<AEnemy>(Impact.GetActor());
if (Enemy)
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("You hit an enemy"));
Enemy->Destroy(true);
}
}
I thought that this should destroy the enemy in the array because they are the same. However when debugging it says that all objects in Spawner array are still there.
It doesn’t work that way. If you destroy an actor in an array, all you have now is an array containing destroyed actors. What might work is if you make an array of TWeakObjectPtrs to your actors. Then, if nothing else is holding a reference to said actors, the weak pointers will be invalidated when they get garbage collected. But that wouldn’t be very reliable either because calling Destroy on an actor doesn’t mean it is instantly invalidated.
What you should do is monitor when these actors are killed/destroyed and manually remove them from the array. You could do this by overriding Destroyed in your enemy class and having it phone home, or having AQuests bind a delegate to AActor::OnDestroyed when adding them to your array.
How do I use this delegate? I have never used them before and I am confused by how to use them.
This is what I have so far in quests class. I’v read documentation on them but I still have no idea what to do with it.
DECLARE_DELEGATE_OneParam(FEnemyDestroyed, FActorDestroyedSignature);
UPROPERTY(BlueprintAssignable)
FEnemyDestroyed OnEnemyDestroyed;