How to get other pawn?

I am trying to have my throwable items deal damage to other pawns and once it hits another pawn and the damage is dealt, the item will disappear, using Destroy().
Currently I am using the ApplyDamage function and leave the DamagedActor as OtherActor, which including the floor, walls, and such. Is there anyway that I can get all other pawn except the one I am controlling, so I can pass it into ApplyDamage()?

void AItem::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	
	APawn* PlayerPawn = GetWorld()->GetFirstPlayerController()->GetPawn();
	SetOwner(PlayerPawn);
	if (PlayerPawn == nullptr) return;
	auto MyOwnerInstigator = PlayerPawn->GetInstigatorController();
	auto DamageTypeClass = UDamageType::StaticClass();
	if (OtherActor && OtherActor != this, OtherActor != PlayerPawn)
	{
		UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwnerInstigator, PlayerPawn, DamageTypeClass);

	}

}
	

Thanks!

Btw, I also tried to use GetAllActorsWithTag function but ApplyDamage does not allow an array.
Would appreciate if there is any suggestion

Hi, I’m not sure I understand what you want :slight_smile:

Is there anyway that I can get all other pawn except the one I am controlling, so I can pass it into ApplyDamage()?

If you want to apply damage to all the pawns except the one you’re controlling, you can get them all (e.g. via GetAllActorsOfClass(WithTag) like you’ve said above) and then loop through the array and call ApplyDamage on each of them.

If you meant, that you want to apply damage only when the OtherActor in your OnHit function is not a specific pawn, then I guess the way you’re checking it in your code above should work.

I have been writing C code for around about 35 years now, and I’ve never seen anyone use the comma operator in such a way (inside the ‘if’). Is that valid, and does it work like that?

… you want a thrown item to deal damage to all other pawns? are you sure that you’re explaining this correctly?

Hi, and yes, that is what I meant. Sorry for the confusion there.
I just want to deal damage to other pawns when throwing items at them, and I have been trying to figure out what I can do since ApplyDamage does not accept array.
I will try looping through the actors instead, thank you for the advice!

so if I throw a thing at pawn A, pawns A, B, and C all take damage?

No, only pawn A will be damaged. Sorry for the confusion.
It is like throwing a rock at another player, the rock will only damages the player it hits.

So I found a way around it by only using tag.
I created a tag for each pawn and twisted the “if” statement I implemented:

if (!OtherActor->ActorHasTag(FName("Pawns"))) return;

if (OtherActor && OtherActor != this && OtherActor != PlayerPawn)
{
			UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwnerInstigator, PlayerPawn, 
            DamageTypeClass);
			UE_LOG(LogTemp, Warning, TEXT("Dealing damage"));
			Destroy();
	 
}

Its not pretty but it works

… cast it to pawn to see if it’s a pawn.

if (Cast<APawn*>(OtherActor) != nullptr && OtherActor != PlayerPawn)

also, you should probably set the owner of it when you spawn it, rather than when it hits something. Then you can just query the owner when it hits something to find out who threw it.

I can’t imagine there’s any good reason to tie the thrown item to GetFirstPlayerController’s pawn.