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:

Download the entire source code from my GitHub repo

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!