Problem with OnTargetPerceptionUpdated, cant figure out how it work

Hello everybody,

I have a big problem, i try access this Function in my AIController Class like this:

Base_AI_Controller.h:

#include "AIController.h"
#include "Base_AI_Controller.generated.h"

UCLASS()
class BEEFENDER_API ABase_AI_Controller : public AAIController
{
	GENERATED_BODY()

	ABase_AI_Controller();


	class UAIPerceptionComponent* AIPerceptionComp;	
	class UAISenseConfig_Sight* Sight;
	class UAISenseConfig_Hearing* Hear;

	UFUNCTION()
	void OnTargetPerceptionUpdated(AActor* SensedActor, FAIStimulus Stimulus);
};

Base_AI_Controller.cpp:

#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"
#include "Perception/AISenseConfig_Hearing.h"
#include "Base_AI_Controller.h"


ABase_AI_Controller::ABase_AI_Controller()
{
	AIPerceptionComp = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("AIPerceptionComp"));

	Sight = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sigth Config"));
	Sight->SightRadius = 400;
	Sight->LoseSightRadius = 600;
	Sight->PeripheralVisionAngleDegrees = 180;
	Sight->DetectionByAffiliation.bDetectEnemies = true;
	Sight->DetectionByAffiliation.bDetectFriendlies = true;
	Sight->DetectionByAffiliation.bDetectNeutrals = true;
	Sight->SetMaxAge(5);
	AIPerceptionComp->ConfigureSense(*Sight);

	Hear = CreateDefaultSubobject<UAISenseConfig_Hearing>(TEXT("Hearing Config"));
	Hear->HearingRange = 1000;
	Hear->DetectionByAffiliation.bDetectEnemies = true;
	Hear->DetectionByAffiliation.bDetectFriendlies = true;
	Hear->DetectionByAffiliation.bDetectNeutrals = true;
	AIPerceptionComp->ConfigureSense(*Hear);

	AIPerceptionComp->OnTargetPerceptionUpdated.AddDynamic(this, &ABase_AI_Controller::OnTargetPerceptionUpdated);
	
}

void ABase_AI_Controller::OnTargetPerceptionUpdated(AActor* SensedActor, FAIStimulus Stimulus)
{
	UE_LOG(LogTemp, Warning, TEXT("Hab was"));
}

when i do this, i get this errors:

Error C2079 ā€˜Z_Param_Stimulus’ uses undefined struct ā€˜FAIStimulus’ Beefender D:\UE4 Projects\Beefender\Source\Beefender\AI\Base_AI_Controller.h 14

Error C2664 ā€˜void ABase_AI_Controller::OnTargetPerceptionUpdated(AActor *,FAIStimulus)’: cannot convert argument 2 from ā€˜int’ to ā€˜FAIStimulus’ Beefender D:\UE4 Projects\Beefender\Source\Beefender\AI\Base_AI_Controller.h 14

Error C2079 ā€˜Z_Construct_UFunction_ABase_AI_Controller_OnTargetPerceptionUpdated::Base_AI_Controller_eventOnTargetPerceptionUpdated_Parms::Stimulus’ uses undefined struct ā€˜FAIStimulus’ Beefender D:\UE4 Projects\Beefender\Intermediate\Build\Win64\UE4Editor\Inc\Beefender\Beefender.generated.cpp 228

I hope you can help me with this :slight_smile:

Sorry for my bad english

1 Like

I just fixed it… stupid me xD I just #include ā€œPerception/AIPerceptionTypes.hā€ in the header file. Now it works fine, but is there another way? cause normaly i have all include in the cpp file. For classes i made forwarddeclarations, is ther a way to do it with structs?

2 Likes

For people from the future, you can’t forward-declare a struct unless it’s a pointer to a struct. Pointers aren’t technically that actual class – it’s just an object pointing to where that class is stored in memory. Structs (in Unreal) are usually references, so the compiler has to know what’s in that struct and how much memory it needs to take up at compile time. Because of that, there’s no way to forward-declare a struct (unless you want to use it as a pointer, which is possible but not really compatible with Blueprints).

1 Like

Necroing this because I think this is untrue.
You can perfectly forward-declare structs the same way you do with classes.
Just fwd-declare like this for example:

// Includes....

struct FAIStimulus; // You can do this!

UCLASS()
class MY_API MyClass : public AAIController
{
    // Stuff...
}
1 Like

Nice Forward declaring FAIStimulus as a struct worked for me.