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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

UAIPerception OnPerceptionUpdated not working

Hello, So I have been working with the perception component but and I have configured a few senses, and then i try to use the OnPerceptionUpdated but the print statement does not work. This is what I have

EDIT

This is my custom controller class possess function called MobAI:

 void AMobAI::Possess(APawn* InPawn) {
     Super::Possess(InPawn);
     Mob = Cast<AMobCharacter>(InPawn);
     SetSenses();
     GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this, &AMobAI::UpdatePerception);
     
 }

This is the function SetSenses():

 void AMobAI::SetSenses() {
     Cast<UMobAIPerceptionComponent>(GetPerceptionComponent())->SetSightConfig(Mob);
     Cast<UMobAIPerceptionComponent>(GetPerceptionComponent())->SetHearingConfig(Mob);
     Cast<UMobAIPerceptionComponent>(GetPerceptionComponent())->SetTouchConfig(Mob);
 }


This is the custom PerceptionComponent called MobAIPerceptionComponent:

 UMobAIPerceptionComponent::UMobAIPerceptionComponent(const FObjectInitializer& ObjectInitializer)
     : Super(ObjectInitializer)
 {
     sightConfig = ObjectInitializer.CreateDefaultSubobject<UMobAISenseConfig_Sight>(this, TEXT("Sight Config"));
     hearingConfig = ObjectInitializer.CreateDefaultSubobject<UMobAISenseConfig_Hearing>(this, TEXT("Hearing Config"));
     touchConfig = ObjectInitializer.CreateDefaultSubobject<UMobAISenseConfig_Touch>(this, TEXT("Touch Config"));
 }
 void UMobAIPerceptionComponent::SetSightConfig(AMobCharacter* Mob) {
     sightConfig->SetConfig(Mob->Sight, Mob->Sight + (Mob->Sight / 2), 65, true, true, true);
     ConfigureSense(*sightConfig);
 }
 void UMobAIPerceptionComponent::SetHearingConfig(AMobCharacter* Mob) {
     hearingConfig->SetConfig(Mob->Hear, true, true, true);
     ConfigureSense(*hearingConfig);
 }
 void UMobAIPerceptionComponent::SetTouchConfig(AMobCharacter* Mob) {
     ConfigureSense(*touchConfig);
     SetDominantSense(touchConfig->GetSenseImplementation());
 }


And finally the custom sense config class (it is essentially the same for all three):

 void UMobAISenseConfig_Sight::SetConfig(float Radius, float loseRadius, float VisionAngle, bool detectEnemies, bool detectFreindly, bool detectNuetral) {
     SightRadius = Radius;
     LoseSightRadius = loseRadius;
     PeripheralVisionAngleDegrees = VisionAngle;
     DetectionByAffiliation.bDetectEnemies = detectEnemies;
     DetectionByAffiliation.bDetectFriendlies = detectFreindly;
     DetectionByAffiliation.bDetectNeutrals = detectNuetral;
 }



When I use the gameplay debugger (I have actually never used it before, so bear with me) this is what happens:

alt text

EDIT So with Perception enabled it clearly shows the character is within range of both sight and hearing.

EDIT

I tried using:

 TArray<AActor*> actors;
     GetPerceptionComponent()->GetPerceivedActors(Cast<UMobAIPerceptionComponent>(GetPerceptionComponent())->hearingConfig->GetSenseImplementation(), actors);
     print(FString::FromInt(actors.Num()));

In order to see if anything was being seen, but it showed a empty array. EDIT

Okay so here are the full files: https://www.dropbox.com/sh/pm2qhzg593a303v/AABu_6c20DEnZldlbyjZq84Ia?dl=0

All of the variables are set via blueprint in the editor on the MobCharacter Class, everything else is derived from there.

Thank you,

Product Version: UE 4.13
Tags:
beardebug.png (596.8 kB)
more ▼

asked Oct 19 '16 at 12:06 AM in C++ Programming

avatar image

joelComberiati
112 26 22 28

avatar image Doug E ♦♦ STAFF Oct 19 '16 at 06:24 PM

Hey joelComberiati-

OnPerceptionUpdated is called from UAIPerceptionComponent::ProcessStimuli if the UpdatedActors array is not empty and the AIOwner is not null. ProcessStimuli is called from UAIPerceptionSystem::Tick if the following conditions are met:

 if (bNeedsUpdate || bStimuliDelivered || bSomeListenersNeedUpdateDueToStimuliAging)
         {
             for (AIPerception::FListenerMap::TIterator ListenerIt(ListenerContainer); ListenerIt; ++ListenerIt)
             {
                 check(ListenerIt->Value.Listener.IsValid())
                     if (ListenerIt->Value.HasAnyNewStimuli())
                     {
                         ListenerIt->Value.ProcessStimuli();
                     }
             }
         }

Let me know if you can run the project in debug mode and ensure that both if() statements are begin hit in UAIPerceptionSystem::Tick or if UpdatedActors / AIOwner is empty/null (respectively).

If these events are being triggered and you are still not getting OnPerceptionUpdated called, please include the full changes to your AIPerception class and how you're adding it to your AI in the editor.

avatar image joelComberiati Oct 19 '16 at 08:46 PM

Okay I shall try this when I get home

avatar image joelComberiati Oct 23 '16 at 08:22 PM

When i was running the project in debug mode I was unable to set breakpoints for this the Perception System file due to there being no debug symbols. I am also unable to access these variables because they are local (except the last one which is private)

avatar image joelComberiati Oct 24 '16 at 12:54 AM

Actually I rebuilt the engine and then I found out that they all return false as far as I can tell.

avatar image MieszkoZ STAFF Oct 19 '16 at 08:09 PM

Out of curiosity, does print("Something"); work in UE4 at all? :D

Also, what's the UMobAISenseConfig_Sight? What sense does it represent? Did you implement it on your own? Is this an extension of any of the other senses? Do you have any sources registered for it?

avatar image joelComberiati Oct 19 '16 at 08:47 PM

No it does not, but I use a #define print(text) {...} on top of all of my .cpp files just to make my life easier. UMobAISenseConfig_Sight is a custom class that I implemented as a child of AISenseConfig_Sight (I have one for touch and hearing also just decided to put this one as an example). Obviously it represents the sense sight. And Sources? I do not believe so, maybe that is where the issue is? How would I go about doing that?

avatar image MieszkoZ STAFF Oct 20 '16 at 12:38 PM

I use a #define print(text) {...} on top of all of my .cpp files just to make my life easier.

Bad practice, reduces code portability. Also, brings in confusion when showing the code to other people and you need to explain code that shouldn't need explaining.

Regarding sources, every sense can specify that it wants every pawn to get registered as its sources (`UAISense.bAutoRegisterAllPawnsAsSources`). If you set it to false you'll have to manually call UAIPerceptionSystem::RegisterSourceForSenseClass.

Have you tried using GameplayDebugger to visualize your AI's perception?

avatar image joelComberiati Oct 21 '16 at 08:46 PM

I just got home and I tried out the Gameplay Debugger, I edited my answer, please have a look. I also tried the register the source but it did not work.

avatar image Justin.Dooley Oct 22 '16 at 12:27 AM

When you use the gameplay debugger, keep in mind you need to use Numpad 1-4 not regular 1-4 Even with nothing there you should get the heading on screen.

avatar image joelComberiati Oct 22 '16 at 12:50 AM

yes i tried both, thank you though, and I did not get a heading

avatar image joelComberiati Oct 22 '16 at 12:51 AM

Actually it appears I had num lock off, it is working now

avatar image joelComberiati Oct 24 '16 at 11:34 PM

This is still causing problems, any help would be appreciated

avatar image Doug E ♦♦ STAFF Oct 25 '16 at 01:55 PM

Hey joelComberiati-

I am attempting to reproduce the behavior you've described and I want to ensure that I'm setting up my AI as closely to yours as possible. Could you provide the steps used when setting up your AI class / actor to help me test this issue locally?

avatar image joelComberiati Oct 25 '16 at 05:21 PM

A Blueprint of MobCharacter class (extension of Pawn) is is placed manually. My AI is set up pretty simply in the constructor of this class using:

 AIControllerClass = AMobAI::StaticClass();

And that is all that I have done in my MobCharacter class except for a couple of extra variables and a mesh. In the AI, I put everything in the question already.

avatar image Doug E ♦♦ STAFF Oct 25 '16 at 08:01 PM

Are the additional variables in your character class Sight and Hear shown in the SetSightConfig and SetHearingConfig functions? How are the values of these variables being set? Do you have three separate classes for UAISenseConfig_Sight / Hearing / Touch? If possible could you provide the full classes used for your AI setup.

avatar image joelComberiati Oct 25 '16 at 08:26 PM

Okay, I put a link to the files on drop box, it includes the ai, perception component, character, and all 3 configs. That should have all of the information you need.

avatar image Doug E ♦♦ STAFF Oct 26 '16 at 02:45 PM

Hey Joel-

Can you provide a link to the dropbox to access the files? You can send me a PM on the forums with the download link if privacy is a concern.

avatar image joelComberiati Oct 26 '16 at 03:04 PM

It is at the bottom of the question, I will put it here as well: https://www.dropbox.com/sh/pm2qhzg593a303v/AABu_6c20DEnZldlbyjZq84Ia?dl=0

avatar image joelComberiati Nov 03 '16 at 02:57 AM

Have you checked the files? Does it seem that something is wrong with this?

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

2 answers: sort voted first

Hey joelComberiati-

I was able to test the OnPerceptionUpdated delegate binding using the following steps:

  • Create two new classes (MyPawn class and MyAIController class - code for classes is attached)

  • Add AIModule to my Build.cs file

  • Compile and open project

  • Create blueprint from MyPawn class and place in level

As I walked around the placed pawn, the on screen message and log warning from my Test function printed which indicated that the dynamic binding was being called. In my test I created the perception component and AISenseConfig_Sight as part of the AIController class rather than subclassing them. Let me know if these steps and files help any or if you're still experiencing a problem with your binding setup.

link text

aisensefiles.zip (2.5 kB)
more ▼

answered Nov 03 '16 at 06:08 PM

avatar image joelComberiati Nov 04 '16 at 02:25 AM

Wow it works! Thanks for all the help. Moral of the story is "why subclass when you don't have too".

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

Hey JoelComberiati,

Could it be that your affiliation settings are wrong?
Try:

 sightConfig->DetectionByAffiliation.bDetectEnemies = true;
 sightConfig->DetectionByAffiliation.bDetectNeutrals = true;
 sightConfig->DetectionByAffiliation.bDetectFriendlies = true;

Hope this helps,
Elias

more ▼

answered Oct 19 '16 at 08:17 AM

avatar image

PasteDog
1.2k 24 16 53

avatar image joelComberiati Oct 19 '16 at 05:45 PM

all affiliations are set to true, and it still does not work. I will put the code for the config set up for clarification.

(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