x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Generate collision events when a key is pressed

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?

Product Version: UE 4.20
Tags:
more ▼

asked Feb 14 '19 at 05:20 PM in Using UE4

avatar image

Iberlos
1 1 2

avatar image ThompsonN13 Feb 14 '19 at 11:23 PM

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.

alt text

capture.png (82.5 kB)
avatar image Iberlos Feb 16 '19 at 06:43 PM

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.

avatar image ThompsonN13 Feb 17 '19 at 02:48 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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!

more ▼

answered Feb 20 '19 at 01:52 AM

avatar image

Iberlos
1 1 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Feb 15 '19 at 04:04 AM

avatar image

Iberlos
1 1 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question