How do i initialize a Pawn Sensing Component on C++?

Hello, i’m trying to add a pawn sensing component to my character using C++, i tried using the same method as i did for the projectile movement:

PawnSenses = ObjectInitializer.CreateDefaultSubobject<UPawnSensingComponent>(this, TEXT("Senses"));

But it’s giving me an error:

UObjectGlobals.h(641): error C2027: use of undefined type 'UPawnSensingComponent'
UObjectGlobals.h(641): error C3861: 'StaticClass': identifier not found
UObjectGlobals.h(642): error C2440: 'static_cast' : cannot convert from 'UObject *' to 'UPawnSensingComponent *'

So, how can i initialize it?

UPawnSensingComponent is part of AIModule which is not included by default to the project, to add it, in your project building script (ProjectName.Build.cs in source directory) modify this part like this

  PublicDependencyModuleNames.AddRange(new string[] { 
        "Core", 
        "CoreUObject", 
        "Engine", 
        "InputCore",
        "AIModule"
    });

You add ,“AIModule” to this list, after that your code should build without problems (or else there something more)

Thank you for the reply, but i already have AIModule included:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "Slate", "SlateCore", "AIModule" });

What else could cause this error?

Well you could try including header files where UPawnSensingComponent is declared with #include Perception/PawnSensingComponent.h, or in your header file (note that you got errors there not in cpp) when you use UPawnSensingComponent place “class” before that, like this “class UPawnSensingComponent”

1 Like

I added the #include Perception/PawnSensingComponent.h to my header file and it started working :slight_smile: By including the AIModule, shouldn’t those headers be included automatically? Thank you for your help :slight_smile:

P.S: I posted a question related to the Pawn Sensing Component about the Make Noise function doesn’t work when it’s called from the client, you wouldn’t happen to know anything about this?