# Generate collision events when a key is pressed

**URL:** <https://forums.unrealengine.com/t/generate-collision-events-when-a-key-is-pressed/443413>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [February 14, 2019, 5:20pm UTC](https://forums.unrealengine.com/t/generate-collision-events-when-a-key-is-pressed/443413 "2019-02-14T17:20:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iberlos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Iberlos](https://forums.unrealengine.com/u/Iberlos)\
**Post date:** [February 14, 2019, 5:20pm UTC](https://forums.unrealengine.com/t/generate-collision-events-when-a-key-is-pressed/443413/1 "2019-02-14T17:20:49Z")

</div>

Hello,  
My name is Luis Filipe and I have been working a Top-Down Arena Style hoard mode game for an assignment, it is a C++ project and I am not allowed to use blueprints.  
I have a PickupActor class that should be added to the players inventory component when the “E” key is pressed. To accomplish that I wanted to have a USphereComponent in my Character class that when I press the “E” key would either activate or call a function that generates the collision events that frame. Since some items may already be inside the the sphere when the “E” key is pressed I can’t use the Begin or End overlap events.

One solution I have considered is registering the begin and end overlaps of all pickups by adding and removing their pointers to a map, but that sounds very bad.

Anyone know if there is a way to generate the overlap events this frame for all objects currently overlapping with the Component?

---

<div class="post-metadata">

**Author:** ![ThompsonN13](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThompsonN13](https://forums.unrealengine.com/u/ThompsonN13)\
**Post date:** [February 14, 2019, 11:23pm UTC](https://forums.unrealengine.com/t/generate-collision-events-when-a-key-is-pressed/443413/2 "2019-02-14T23:23:21Z")

</div>

if you were using blueprints (i know you cant but theres C++ behind it) i would suggest the get overlapping actors node. then you could run a loop to check for any actors of your pickup class and add it to your inventory. I dont know c++ but i know anything you can do in blueprint is implemented from C++ its just a matter of finding the right script so it shouldnt be too much trouble to figure out.

 ![268650-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/d/9/6d9b94052c96758b05c0acb0398a1f929bbef305.png)

---

<div class="post-metadata">

**Author:** ![Iberlos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Iberlos](https://forums.unrealengine.com/u/Iberlos)\
**Post date:** [February 15, 2019, 4:04am UTC](https://forums.unrealengine.com/t/generate-collision-events-when-a-key-is-pressed/443413/3 "2019-02-15T04:04:46Z")

</div>

GetOverlappingActors() is a function I can call in the C++ code. It is probably the correct answer. I will test it as soon as possible.

---

<div class="post-metadata">

**Author:** ![Iberlos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Iberlos](https://forums.unrealengine.com/u/Iberlos)\
**Post date:** [February 16, 2019, 6:43pm UTC](https://forums.unrealengine.com/t/generate-collision-events-when-a-key-is-pressed/443413/4 "2019-02-16T18:43:48Z")

</div>

So I am currently trying to make this work, but the GetOverlappingActors returns an empty array…

Here is the generation of the components that should interact with one another:

In PlayerCharacter:

```
APlayerCharacter::APlayerCharacter()
{
        m_PickupReach = CreateDefaultSubobject<USphereComponent>("Pickup Reach");
        m_PickupReach->SetSphereRadius(100.0f);
        m_PickupReach->SetRelativeLocation(FVector(0.0f, 0.0f, 90.0f));
        m_PickupReach->SetupAttachment(RootComponent);
        m_PickupReach->SetGenerateOverlapEvents(true);
        m_PickupReach->SetCollisionProfileName("OverlapAllDynamic");
        m_PickupReach->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
}

void APlayerCharacter::InteractWithActors()
{
    TArray<AActor*> OutActors;
    TSubclassOf<AActor> ClassFilter;
    m_PickupReach->GetOverlappingActors(OutActors, ClassFilter);

    for (int i = 0; i < OutActors.Num(); i++)
    {
        AInteractableActor* interactableActor = Cast<AInteractableActor>(OutActors[i]);
        if (interactableActor)
        {
            interactableActor->Interact(this);
        }
    }
}

```

In IteractableActor:

```
AInteractableActor::AInteractableActor()
{
        m_InteractBounds = CreateDefaultSubobject<USphereComponent>("Interact Bounds");
        m_InteractBounds->SetupAttachment(RootComponent);
        m_InteractBounds->SetGenerateOverlapEvents(true);
        m_InteractBounds->SetCollisionProfileName("OverlapAllDynamic");
        m_InteractBounds->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
}

```

Basically PlayerCharacter::InteractWithActors never enter the for loop because OutActors is empty.

---

<div class="post-metadata">

**Author:** ![ThompsonN13](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThompsonN13](https://forums.unrealengine.com/u/ThompsonN13)\
**Post date:** [February 17, 2019, 2:48pm UTC](https://forums.unrealengine.com/t/generate-collision-events-when-a-key-is-pressed/443413/5 "2019-02-17T14:48:20Z")

</div>

hmmm the script looks good from what i can tell though i dont know C++ or the ue4 api so thats not saying much.

one thing i noticed that im not sure on is

TArray OutActors;

is there supposed to be an asterisk in there? its supposed to be just an array of actors. now i know the asterisk can be a wildcard in some languages so im not sure on the implication here. again though not a C++ guy and its been awhile since i wrote anything in a actual scripting language.

---

<div class="post-metadata">

**Author:** ![Iberlos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Iberlos](https://forums.unrealengine.com/u/Iberlos)\
**Post date:** [February 20, 2019, 1:52am UTC](https://forums.unrealengine.com/t/generate-collision-events-when-a-key-is-pressed/443413/6 "2019-02-20T01:52:37Z")

</div>

Yes the asterisk means it is a pointer to the actor not the actual object.  
I have fixed it. It turns out you can’t have both of these line at the same time:

```
         m_InteractBounds->SetCollisionProfileName("OverlapAllDynamic");
         m_InteractBounds->SetCollisionEnabled(ECollisionEnabled::QueryOnly);

```

Thank you very much for your help Thompson13!
