# Difficulties with SweepMultiByChannel and storing pointers to hit actors.

**URL:** <https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070>\
**Category:** C++\
**Tags:** sweep, arrays, Struct, tarray, unreal-engine, Array, CPP, question\
**Created:** [February 8, 2024, 9:20pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070 "2024-02-08T21:20:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheoRat3](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/theorat3/32/897542_2.png) [@TheoRat3](https://forums.unrealengine.com/u/TheoRat3)\
**Post date:** [February 8, 2024, 9:20pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/1 "2024-02-08T21:20:15Z")

</div>

I’m working on creating an ability that applies a status effect to enemies in the form of attaching actors of a class I made “WeakPoint” to their meshes. I ran into a wall today when trying to apply this ability to an indefinite number of targets. I’m attempting to take all of the actors hit in the OutHIts array, itterate through it and store pointers to the actors that are derived from my Enemy class into its own array, that I then use to add the weakpoints to their respective meshes. I have two enemies currently in the world, and after performing the code below if I UE\_LOG the size of my array for enemies hit by the sweep the number shown is 30… If I UE\_LOG the size of OutHIts the number shown is 68. So clearly my for loop is doing some kind of filtering, but it’s storing duplicate addresses to the same enemy, causing an excess of weak points to be spawned. Below is my code for my sweep and how I’m currently iterating through actors as well as my Struct I’m using to store the weakpoints to the enemies for context.

```auto
FCollisionShape HitSphere = FCollisionShape::MakeSphere(TacticalRange);

bool bHitSuccess = GetWorld()->SweepMultiByChannel(
OutHits, 
PlayerCharacter->GetActorLocation(), 
PlayerCharacter->GetActorLocation(), 
FQuat::Identity, 
ECC_GameTraceChannel1, 
HitSphere);

  if(bHitSuccess)
  {
       for(int32 i = 0; i < OutHits.Num(); i++)
       {

            if (OutHits[i].GetActor()->IsA(AEnemy::StaticClass()))
            {
                FEnemyData EnemyHit = {Cast<AEnemy>(OutHits[i].GetActor())};
                EnemiesHitByTactical.Add(EnemyHit);
               
            }
        }

       UE_LOG(LogTemp, Warning, TEXT("number of enemies hit %i"), 
       EnemiesHitByTactical.Num());
       UE_LOG(LogTemp, Warning, TEXT("number of actors hit %i"), 
       OutHits.Num());
}

```

```auto
USTRUCT(BlueprintType)
struct FEnemyData
{
    GENERATED_BODY()
    
    class AEnemy* EnemyPtr;
    TArray<AWeakPoint*> WeakPointsArr;
    
};

```

---

<div class="post-metadata">

**Author:** ![NeoGothique](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neogothique/32/773410_2.png) [@NeoGothique](https://forums.unrealengine.com/u/NeoGothique)\
**Post date:** [February 9, 2024, 3:05am UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/2 "2024-02-09T03:05:27Z")

</div>

SweepMultiByChannel sweeps for ALL overlaps (ie all components), so it’s possible to get the same actor twice through multiple components.

So you could have a custom trace channel for this purpose and make sure it’s assigned to overlap/block only on the root component of the class you are looking for.

Or make a TArray of actor pointers using AddUnique and then make your structs after the sweep is done being sorted (but I don’t know immediately what the most performant variant of that method would be)

---

<div class="post-metadata">

**Author:** ![TheoRat3](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/theorat3/32/897542_2.png) [@TheoRat3](https://forums.unrealengine.com/u/TheoRat3)\
**Post date:** [February 9, 2024, 6:42pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/3 "2024-02-09T18:42:42Z")

</div>

Do you know how one would go about specifying that a trace channel only overlap/block the root component?

---

<div class="post-metadata">

**Author:** ![NeoGothique](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neogothique/32/773410_2.png) [@NeoGothique](https://forums.unrealengine.com/u/NeoGothique)\
**Post date:** [February 9, 2024, 6:52pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/4 "2024-02-09T18:52:09Z")

</div>

From within the Editor you can go Edit → Project Settings, search up Trace Channels and click New Trace Channel… (and set to Ignore by default)

Once you add it there, it will be present in your ProjectFolder/Config/DefaultEngine.ini as one of the ECC\_GameTraceChannels in this kind of format:

`+DefaultChannelResponses=(Channel=ECC_GameTraceChannel1,DefaultResponse=ECR_Block,bTraceType=False,bStaticObject=False,Name="Projectile")`

Then just replace your ECC\_GameTraceChannel1 in your example with the corresponding number and change the collision profile of the component of your choice to start overlapping or blocking the new trace channel you created.

---

<div class="post-metadata">

**Author:** ![TheoRat3](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/theorat3/32/897542_2.png) [@TheoRat3](https://forums.unrealengine.com/u/TheoRat3)\
**Post date:** [February 9, 2024, 8:18pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/6 "2024-02-09T20:18:44Z")

</div>

I tried implementing a capsule component on the enemy base class like so:

```auto
AEnemy::AEnemy()
{
	PrimaryActorTick.bCanEverTick = true;

	EnemyIdentifier = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Enemy 
    Identifier"));
	EnemyIdentifier->InitCapsuleSize(5.f, 5.f);
	EnemyIdentifier->SetupAttachment(RootComponent);
	EnemyIdentifier->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	EnemyIdentifier->SetCollisionResponseToChannel(ECC_GameTraceChannel2, 
        ECR_Overlap);

}

```

And now no collision is being detected by my sweep which looks the same as before with the exception of changing the channel name

```auto
bool bHitSuccess = GetWorld()->SweepMultiByChannel(OutHits, PlayerCharacter->GetActorLocation(), PlayerCharacter->GetActorLocation(), FQuat::Identity, ECC_GameTraceChannel2, HitSphere);

```

But weirdly enough while messing with the capsule component I made in the constructor at a certain point I went from being able to edit and view the component in my Zombie blueprint, to seeing two versions of the component, to then only one again except now when I click on it the details window is blank. Any idea what’s going on?

 ![Screenshot (808)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/f/2/7f2711b99424bcd2de2900a1a224fc03217ef7a9.png)

![Screenshot (809)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/6/a/a6a23953dedde39025ef6dc65f3146e29f552108.png)

 ![Screenshot (810)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/8/f/f8f6f387af3f87ff6042fbd43517ebffa6e0503e.png)

---

<div class="post-metadata">

**Author:** ![NeoGothique](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neogothique/32/773410_2.png) [@NeoGothique](https://forums.unrealengine.com/u/NeoGothique)\
**Post date:** [February 9, 2024, 8:50pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/7 "2024-02-09T20:50:29Z")

</div>

If it has nothing in details pane, that is usually because you are missing the right UPROPERTY specifiers for the component (like EditAnywhere, BlueprintReadWrite) or might need to restart the editor and compile if using live coding

---

<div class="post-metadata">

**Author:** ![TheoRat3](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/theorat3/32/897542_2.png) [@TheoRat3](https://forums.unrealengine.com/u/TheoRat3)\
**Post date:** [February 9, 2024, 8:56pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/8 "2024-02-09T20:56:41Z")

</div>

Sorry to hound you, but do you see something wrong with how I tried to implement the new collision component? The hit is no longer successful.

---

<div class="post-metadata">

**Author:** ![NeoGothique](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neogothique/32/773410_2.png) [@NeoGothique](https://forums.unrealengine.com/u/NeoGothique)\
**Post date:** [February 9, 2024, 9:31pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/9 "2024-02-09T21:31:32Z")

</div>

I believe it is because SweepMultiByChannel only returns True if it encountered a blocking hit, so you are testing out based on that condition. You can just remove that test and only worry about the OutHits, which will be empty if there are no overlaps.

If you use blocking hits, the first blocking hit encountered will end the sweep, so you want to use only overlaps if you want to ensure you get everything in the region you’re sweeping against.

---

<div class="post-metadata">

**Author:** ![TheoRat3](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/theorat3/32/897542_2.png) [@TheoRat3](https://forums.unrealengine.com/u/TheoRat3)\
**Post date:** [February 9, 2024, 9:42pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/10 "2024-02-09T21:42:04Z")

</div>

When you say remove that test you mean take out SweepMultiByChannel entirely? What would be used in place of it that can sweep in a area all at once? I’m new to using Unreal engine so I only know about this method and line tracing.

---

<div class="post-metadata">

**Author:** ![NeoGothique](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neogothique/32/773410_2.png) [@NeoGothique](https://forums.unrealengine.com/u/NeoGothique)\
**Post date:** [February 9, 2024, 9:49pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/11 "2024-02-09T21:49:13Z")

</div>

```cpp
FCollisionShape HitSphere = FCollisionShape::MakeSphere(TacticalRange);

GetWorld()->SweepMultiByChannel(
	OutHits,
	PlayerCharacter->GetActorLocation(),
	PlayerCharacter->GetActorLocation(),
	FQuat::Identity,
	ECC_GameTraceChannel1,
	HitSphere);

for (int32 i = 0; i < OutHits.Num(); i++)
{

	if (OutHits[i].GetActor()->IsA(AEnemy::StaticClass()))
	{
		FEnemyData EnemyHit = { Cast<AEnemy>(OutHits[i].GetActor()) };
		EnemiesHitByTactical.Add(EnemyHit);

	}
}

UE_LOG(LogTemp, Warning, TEXT("number of enemies hit %i"),
	EnemiesHitByTactical.Num());
UE_LOG(LogTemp, Warning, TEXT("number of actors hit %i"),
	OutHits.Num());

```

You can just remove the check against the bool you are assigning to the return value of SweepMultiByChannel, there’s no need to use it if you only care about overlaps

---

<div class="post-metadata">

**Author:** ![TheoRat3](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/theorat3/32/897542_2.png) [@TheoRat3](https://forums.unrealengine.com/u/TheoRat3)\
**Post date:** [February 9, 2024, 10:17pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/12 "2024-02-09T22:17:06Z")

</div>

The UE\_LOG still says that the contents of OutHits is 0. I don’t know why but it’s still not detecting the enemies in the sweep. It seems like there’s something wrong with my Enemy base class. Do I need OnOverlapBegin and end functions or something??

---

<div class="post-metadata">

**Author:** ![TheoRat3](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/theorat3/32/897542_2.png) [@TheoRat3](https://forums.unrealengine.com/u/TheoRat3)\
**Post date:** [February 9, 2024, 10:43pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/13 "2024-02-09T22:43:52Z")

</div>

I ultimately remedied the issue by going into my Enemy base class and putting the following code in the constructor

```auto
AEnemy::AEnemy()
{
 	
	PrimaryActorTick.bCanEverTick = true;

	GetCapsuleComponent()- 
        >SetCollisionResponseToChannel(ECollisionChannel::ECC_GameTraceChannel2, 
         ECR_Overlap);

}

```

Apparently I was overcomplicating things trying to make it my own custom capsule collision. I still wish I could’ve figured out how to make it work that way just for future reference. But At least now I can move on to the next issue.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [March 10, 2024, 10:43pm UTC](https://forums.unrealengine.com/t/difficulties-with-sweepmultibychannel-and-storing-pointers-to-hit-actors/1675070/14 "2024-03-10T22:43:52Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
