# For loop when given character Hit Result triggers multiple times

**URL:** <https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068>\
**Category:** Character & Animation\
**Tags:** for-loop, problems, unreal-engine, question\
**Created:** [September 9, 2020, 11:42am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068 "2020-09-09T11:42:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 9, 2020, 11:42am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/1 "2020-09-09T11:42:05Z")

</div>

I this is in a replicated Function on the character

**the problem is that the TakeDamage is called around 10 times making the character die instantly but when i trace for other objects like the FLOOR it only prints the string 1 time like it should**

I want the take damage function to only call once for the character

```
TArray<AActor*> ActorsToIgnore;

AActor* SourceActor = this;
if (!SourceActor)
{
	// Hero is dead
	return;
}

 // this is use for other reasons
ActorsToIgnore.Add(SourceActor);

FCollisionQueryParams Params;
//Params.AddIgnoredActors(ActorsToIgnore);

TArray<FHitResult> HitResult;
FVector Start = RootComponent->GetComponentLocation();

GetWorld()->SweepMultiByChannel(HitResult, Start, Start, FQuat::Identity, ECC_GameTraceChannel4, FCollisionShape::MakeSphere(500.f), Params);

for (auto& LoopHitResult : HitResult)
{
	if (LoopHitResult.GetActor() == SourceActor)
	{
		LoopHitResult.Actor->TakeDamage(20.f, FDamageEvent(), GetController(), this);

		GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Black, FString::Printf(TEXT("Hit: %s"), *LoopHitResult.Actor->GetName()));
	}
}

```

---

<div class="post-metadata">

**Author:** ![CHADALAK1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CHADALAK1](https://forums.unrealengine.com/u/CHADALAK1)\
**Post date:** [September 9, 2020, 9:26pm UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/2 "2020-09-09T21:26:43Z")

</div>

Could try checking a bit directly:

```
if (LoopHitResult.GetActor()->IsA(YourActorClass::StaticClass())
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 9, 2020, 10:19pm UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/3 "2020-09-09T22:19:42Z")

</div>

Yes but why is the loop going through my character 10 times every time the function is called but when it is the floor it only goes through once

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 9, 2020, 11:02pm UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/4 "2020-09-09T23:02:50Z")

</div>

**This is what prints on screen**

 ![313000-annotation-2020-09-10-090046.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/9/7/797c5e3c67aa345c36041eb48618868fabe4b940.png)

---

<div class="post-metadata">

**Author:** ![AnotherZach](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AnotherZach](https://forums.unrealengine.com/u/AnotherZach)\
**Post date:** [September 9, 2020, 11:07pm UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/5 "2020-09-09T23:07:10Z")

</div>

You should consider drawing a debug sphere where you trace it, see how it looks in the scene, and check the collision settings of your components.

Also, it might be that both the actor and mesh components each have an associated FHitResult. You can confirm this by printing the display name of each primitive component for all the hit results.

A dirty way to solve this problem is just have a break statement after you trigger the damage once. This ensures it will only happen once, regardless of whats happening behind the scenes.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 10, 2020, 12:18am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/6 "2020-09-10T00:18:19Z")

</div>

I made a new sphere component attached to the character , this component is the only one in the game that has the custom collision channel That the SphereTraceMulti traces for. but it still happens more then once **it only happens twice now not 10 but i only want it happening 1 time**

![313024-annotation-2020-09-10-101302.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/d/4/fd429f2e64d91ec5b346d919a56d5e1a333218be.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 10, 2020, 12:20am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/7 "2020-09-10T00:20:32Z")

</div>

**i did what you said and printed out the components but CollisionCylinder still happens twice**

---

<div class="post-metadata">

**Author:** ![janimationd](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@janimationd](https://forums.unrealengine.com/u/janimationd)\
**Post date:** [September 10, 2020, 2:43am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/8 "2020-09-10T02:43:36Z")

</div>

Is your character’s collisions settings set to block or overlap with the ECC\_GameTraceChannel4 channel? You can check in your project’s collision settings. I would expect to see this sort of behavior if it was set to overlap rather than block.

Also is there a reason you’re calling SweepMultiByChannel and not something simpler?

---

<div class="post-metadata">

**Author:** ![janimationd](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@janimationd](https://forums.unrealengine.com/u/janimationd)\
**Post date:** [September 10, 2020, 2:50am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/9 "2020-09-10T02:50:01Z")

</div>

A side comment, this check will never be true, and so that return will never be run.

```
AActor* SourceActor = this;
 if (!SourceActor)
 {
     // Hero is dead
     return;
 }

```

`this` will never be null unless you’re corrupting memory or something. If you want to see if the actor/component has had Destroy\*() called recently you can instead call IsPendingKill(): [UObjectBaseUtility::IsPendingKill | Unreal Engine Documentation](https://docs.unrealengine.com/en-US/API/Runtime/CoreUObject/UObject/UObjectBaseUtility/IsPendingKill/index.html)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 10, 2020, 3:03am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/10 "2020-09-10T03:03:55Z")

</div>

yes collision is for **Overlap**

All i am trying to do is find all characters near my character and apply Take damage on them

i am not sure if having a sphere component and checking for overlaps

or

doing SweepMultiByChannel is best option as i am searching for efficient option

---

<div class="post-metadata">

**Author:** ![AnotherZach](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AnotherZach](https://forums.unrealengine.com/u/AnotherZach)\
**Post date:** [September 10, 2020, 6:19pm UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/11 "2020-09-10T18:19:27Z")

</div>

I’m going to assume its happening twice without a break statement. Imagine you have two circles that intersect each other. They will share two points, if it isn’t the edge case. In the 3D case you will get a surface overlap, but it will just be points if you are dealing with unfilled volumes.

I very much recommend drawing the sphere with DrawDebugSphere() from “DrawDebugHelpers.h”. You will see the overlap points. Then post the picture. Text output is useful, but it really helps to see the traces.

---

<div class="post-metadata">

**Author:** ![janimationd](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@janimationd](https://forums.unrealengine.com/u/janimationd)\
**Post date:** [September 10, 2020, 10:34pm UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/12 "2020-09-10T22:34:07Z")

</div>

If you read the documentation on [SweepMultiByChannel](https://docs.unrealengine.com/en-US/API/Runtime/Engine/Engine/UWorld/SweepMultiByChannel/index.html) you’ll notice that it says “return all initial overlaps using a specific channel (including blocking) if requested, then overlapping hits and then first blocking hit”. Since your character only overlaps with this trace channel that means your sweep is going to keep passing through your character, returning an event for every physical body in the character that it overlaps with. If you want to only have one event be returned, either set the trace channel to block on your character or use a different trace/sweep function.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 10, 2020, 10:48pm UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/13 "2020-09-10T22:48:57Z")

</div>

thanks

but i have multiple meshes on my characters so i only set the capsule component to block the custom collision channel but it still happens 2 times

---

<div class="post-metadata">

**Author:** ![janimationd](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@janimationd](https://forums.unrealengine.com/u/janimationd)\
**Post date:** [September 11, 2020, 12:06am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/14 "2020-09-11T00:06:49Z")

</div>

Maybe you can check to see which components/bones are in each hit result returned. That might provide some clues as to what is going on. You can then use that information to double check all your collision settings on each bone/component, you might have multiple problems going on.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 11, 2020, 12:09am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/15 "2020-09-11T00:09:31Z")

</div>

it is very odd because i am using UE4 Shooter Game demo and it is doing this but on other demos don’t do it

---

<div class="post-metadata">

**Author:** ![janimationd](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@janimationd](https://forums.unrealengine.com/u/janimationd)\
**Post date:** [September 11, 2020, 12:12am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/16 "2020-09-11T00:12:11Z")

</div>

I think I understand what you’re trying to do a bit better now. Unreal’s damage system actually has a built in function to do what you’re trying to do, it’s called [Apply Radial Damage](https://answers.unrealengine.com/questions/81380/getting-an-explosion-effect-to-cause-damage.html).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 11, 2020, 12:19am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/17 "2020-09-11T00:19:29Z")

</div>

this would be the correct answer but i also want to do other things that can only be done if i **get an array of the hit characters**

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 11, 2020, 12:29am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/18 "2020-09-11T00:29:27Z")

</div>

Bellow is the debug messages

![313155-annotation-2020-09-11-102430.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/b/e/cbe154aa3b82946c67aaf66746b44dea46a8d3d4.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 13, 2020, 8:09am UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/19 "2020-09-13T08:09:46Z")

</div>

# i got it working with this

```
TArray<FHitResult> OutHits;
FCollisionQueryParams Params;
FVector Start = GetRootComponent()->GetComponentLocation();
FVector End = Start + 0.1f;
FCollisionShape CS;
CS.MakeSphere(1000.f);
CS.SetSphere(1000.f);

if (GetWorld()->SweepMultiByObjectType(OutHits, Start, End, FQuat::Identity, FCollisionObjectQueryParams(ECC_Pawn), CS, Params))
{
	int32 Segments = 1;
	DrawDebugSphere(GetWorld(), Start, CS.GetSphereRadius(), Segments, FColor::Blue, true);
}

TArray<AShooterCharacter*> CharactersHit;

for (const FHitResult& ArrayElements : OutHits)
{
	int32 Segments = 1;
	DrawDebugSphere(GetWorld(), ArrayElements.ImpactPoint, 20.f, Segments, FColor::Blue, true);

	AShooterCharacter* HitCharac = Cast<AShooterCharacter>(ArrayElements.GetActor());
	CharactersHit.AddUnique(HitCharac);
}

```

#But when i add this

```
for (const AShooterCharacter* const ArrayElements : CharactersHit)
{
	const FString& PlayerName = ArrayElements->GetActorLabel();
	GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Blue, FString::Printf(TEXT("character hit: %s"), *PlayerName));
}

```

# it crashes

**and i need this to iterate through the refined CharactersHit which has no duplicates from AddUnique()**

---

<div class="post-metadata">

**Author:** ![janimationd](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@janimationd](https://forums.unrealengine.com/u/janimationd)\
**Post date:** [September 13, 2020, 3:58pm UTC](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068/20 "2020-09-13T15:58:06Z")

</div>

You should be able to see where the crash happened in the stacktrace in the crash popup window. Alternatively you could build the visual studio project for “Development Editor” target and debug locally, then recreate the crash. I suspect it might be the case that your cast to AShooterCharacter might be failing, leading to dereferencing null pointers later on.

[Next page](https://forums.unrealengine.com/t/for-loop-when-given-character-hit-result-triggers-multiple-times/471068.md?page=2)
