skip to Main Content

Creating custom AI senses

In this post I’m going to show you how to create your own custom AI senses. For demonstration purposes I’m going to create a new AI sense named “Aquaphobia” which will be responsible for identifying nearby water resources and register stimulus for each wate resouce. Then, the controller of the AI pawn will move the pawn away from these resources. Here’s the end result of the blog post:

This post was created using the 4.21 version of the engine. Depending on the version you’re using you may need to adjust the demonstrated code to match the corresponding API.

In order to add a new, custom AI sense in the engine we really need two classes:

  • The AI sense class, which will contain the core functionality regarding the logic of the sense
  • The config class, which will contain the properties that we’re going to use for the AI sense

With that said, add two new class, named AISense_Aquaphobia and AISenseConfig_Aquaphobia which will inherit the AISense and AISenseConfig classes respectively.

Creating the sense config

Now that we have inherited the correct classes, provide the following logic inside your config class:

Then, add the following logic for the source file:

We’re done with the config class for our case so let’s move on to the core logic for our sense

Creating the actual sense

Here are the whole header and source files needed for our simple AI sense for this post:

At this point we’re almost. Once you compile your code you need to create an AI pawn and AI controller who was the “AIPerception” component. Then, you can assign the sense config and the sense itself like it was displayed in the video at the start of the post.

The sense we have created is only responsible for registering stimulus to each listener. In order to determine where the pawn needs to move every time we sense a water resource I created the following blueprint code inside the AI controller to reduce the overall code need for this post:

At this point, don’t forget to add a nav mesh to your level and tag some actors as “AquaActor” so you can test the functionality of the sense. Moreover, please note that you may have to restart the engine so your AI sense can be configured correctly.

Thanks for reading!

Avatar photo

This Post Has 3 Comments

  1. I do this lesson, but there are errors
    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol “public: __cdecl UAISenseConfig_Aquaphobia::UAISenseConfig_Aquaphobia(class FObjectInitializer const &)” (??0UAISenseConfig_Aquaphobia@@QEAA@AEBVFObjectInitializer@@@Z) referenced in function “public: static void __cdecl UAISenseConfig_Aquaphobia::__DefaultConstructor(class FObjectInitializer const &)” (?__DefaultConstructor@UAISenseConfig_Aquaphobia@@SAXAEBVFObjectInitializer@@@Z) AquaFobia1 D:\UnrealEngine4_Project\AquaFobia1\Intermediate\ProjectFiles\AISenseConfig_Aquaphobia.cpp.obj 1

  2. Hi, I used to work with blueprints but there is no option to create custom eqs tests, so i decided to learn a little bit of C++ and I wonder if you could make a tutorial about it. Thanks ! PS: Sorry for language, I am a French student.

  3. I have for example UStaticMeshComponent in a parent class and I wish to override it with another mesh in a child class. I made blueprints for both the parent class and the child class. I’m asking because I cannot attach the child class object to a socket (in my player class). I can only do that with the parent class. How to properly get child components and attach to a socket in a player.
    ShapeClass -> CubeClass
    ShapeClass->SphereClass
    I can attach the ShapeClass, but not the CubeClass or SphereClass.

    ChildComponent->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, SocketName);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top