# How to get other pawn?

**URL:** <https://forums.unrealengine.com/t/how-to-get-other-pawn/504602>\
**Category:** C++\
**Tags:** unreal-engine, UE4-27, question\
**Created:** [March 7, 2022, 3:47am UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602 "2022-03-07T03:47:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HHX1602](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HHX1602](https://forums.unrealengine.com/u/HHX1602)\
**Post date:** [March 7, 2022, 3:47am UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602/1 "2022-03-07T03:47:33Z")

</div>

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()?

```auto
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!

---

<div class="post-metadata">

**Author:** ![HHX1602](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HHX1602](https://forums.unrealengine.com/u/HHX1602)\
**Post date:** [March 7, 2022, 9:05am UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602/2 "2022-03-07T09:05:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chrudimer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chrudimer/32/918651_2.png) [@chrudimer](https://forums.unrealengine.com/u/chrudimer)\
**Post date:** [March 7, 2022, 10:59am UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602/3 "2022-03-07T10:59:58Z")

</div>

Hi, I’m not sure I understand what you want 🙂

> 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.

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [March 7, 2022, 11:52pm UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602/4 "2022-03-07T23:52:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![HHX1602](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HHX1602](https://forums.unrealengine.com/u/HHX1602)\
**Post date:** [March 8, 2022, 2:10am UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602/5 "2022-03-08T02:10:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [March 8, 2022, 3:07am UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602/6 "2022-03-08T03:07:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![HHX1602](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HHX1602](https://forums.unrealengine.com/u/HHX1602)\
**Post date:** [March 8, 2022, 6:13am UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602/8 "2022-03-08T06:13:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![HHX1602](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HHX1602](https://forums.unrealengine.com/u/HHX1602)\
**Post date:** [March 8, 2022, 6:19am UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602/9 "2022-03-08T06:19:00Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [March 8, 2022, 1:32pm UTC](https://forums.unrealengine.com/t/how-to-get-other-pawn/504602/10 "2022-03-08T13:32:03Z")

</div>

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

```auto
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.
