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"

How do you create a delegate to add to PerceptionComponent->OnPerceptionUpdated

Hi,

I looked around and found a couple really useful threads about how to set up AI Perception in c++. The one thing im getting stuck on however is creating my own function to get called with OnPerceptionUpdated. What I have now has no errors it just isn't calling my debug message when I enter into my AI's sight perception. So I assume that My SenseUpdated function is not being called when you would expect it should be.

Heres the .h

 UCLASS()
 class PW_GAME_API APWAIController : public AAIController
 {
     GENERATED_BODY()
 
     APWAIController(const FObjectInitializer& ObjectInitializer);
 
     UAISenseConfig_Sight* sightConfig;
 
     TScriptDelegate<> sense;
 
 public:
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
     float agroRange;
 
     UFUNCTION(Category = "Perception")
     void SenseUpdated(TArray<AActor*>& testActors);
 };

and heres the .cpp

 APWAIController::APWAIController(const FObjectInitializer& ObjectInitializer)
     :Super(ObjectInitializer.SetDefaultSubobjectClass<UPWPathFollowingComponent>(TEXT("PathFollowingComponent")))
 {
     agroRange = 500;
 
     PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));
     sightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
 
     PerceptionComponent->ConfigureSense(*sightConfig);
     PerceptionComponent->SetDominantSense(sightConfig->GetSenseImplementation());
 
     sightConfig->SightRadius = agroRange;
     sightConfig->LoseSightRadius = agroRange + 50;
     sightConfig->DetectionByAffiliation.bDetectEnemies = true;
     sightConfig->DetectionByAffiliation.bDetectNeutrals = true;
     sightConfig->DetectionByAffiliation.bDetectFriendlies = true;
 
     sense.BindUFunction(this,"SenseUpdated");
 
     PerceptionComponent->OnPerceptionUpdated.Add(sense);
 }
 
 void APWAIController::SenseUpdated(TArray<AActor*>& Actors)
 {
     GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Blue, "Perception Sense Updated");
 }
Product Version: UE 4.8
Tags:
more ▼

asked Jul 09 '15 at 10:56 PM in C++ Programming

avatar image

Rynold
56 8 9 15

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

1 answer: sort voted first

I Fixed it, kinda silly however I shall post the answer all the same. I did not realize that AddDynamic was a macro and my Intellesense was not showing it therefore I did not think It existed. Once I figured out I could use it, it made things really easy. here's the updated code (Just the cpp):

 APWAIController::APWAIController(const FObjectInitializer& ObjectInitializer)
     :Super(ObjectInitializer.SetDefaultSubobjectClass<UPWPathFollowingComponent>(TEXT("PW PathFollowingComponent")))
 {
     PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));
     sightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
 
     PerceptionComponent->ConfigureSense(*sightConfig);
     PerceptionComponent->SetDominantSense(sightConfig->GetSenseImplementation());
 
     sightConfig->SightRadius = agroRange;
     sightConfig->LoseSightRadius = agroRange + 50;
     sightConfig->DetectionByAffiliation.bDetectEnemies = true;
     sightConfig->DetectionByAffiliation.bDetectNeutrals = true;
     sightConfig->DetectionByAffiliation.bDetectFriendlies = true;
 
     PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &APWAIController::SenseUpdated);
 }
 
 void APWAIController::SenseUpdated(TArray<AActor*> Actors)
 {
     GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Blue, "Perception Sense Updated");
 }
more ▼

answered Jul 10 '15 at 01:11 AM

avatar image

Rynold
56 8 9 15

(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