Creating a Utility AI from Scratch

Introduction

The following tutorial shows you how to create a Utility AI from scratch based on components.

This example is based on using MonoBehaviours / Components. The Apex Utility AI can also be used without MonoBehaviours, or in scenarios where both MonoBehaviours and non-MonoBehaviours are needed.

Part 1: Creating the Utility AI

To set up a Utility AI from scratch the following steps need to be completed:

  1. Create a Context class in C# for the AI
  2. Create a Context Provider component i C# that implements the IContextProvider interface and add this to the GameObject
  3. Add a UtilityAIComponent to the GameObject
  4. Create a Utility AI in the Utility AI Editor and add this to the UtilityAIComponent

Once these steps have been completed, you have an empty, working Utility AI.

Step 1: Create a Context class in C# for the AI

The Context is the class that stores the memory, the sensor input, and any other information the AI needs to take decisions. The context is thus different from game to game, and games can feature several different contexts for different AIs.

In the following example, we create a simple Context object that takes the transform of the AI as a parameter upon construction, and creates a new list to store the game objects of observed units.

The class looks as follows:


The Context class must implement the IAIContext interface to be picked up by the Utility AI. In the constructor, arguments can be passed, such as the transform associated with the AI. In addition, lists and other objects can be instantiated, such as the list of observed enemies stored via their colliders.

Step 2: Create a Context Provider component in C# that implements the IContextProvider interface and add this to the GameObject

The Context Provider is a class that holds a reference to the Context that the Utility AI can access every time the AI is run. The context provider has to implement the IContextProvider interface and be on the same GameObject as the UtilityAIComponent.
The Context Provider class looks as follows:


The Context Provider instantiates a new Context in the Awake() method. Subsequently, the Utility AI uses this context whenever it runs.

Step 3: Add a UtilityAIComponent to the GameObject

Add a UtilityAIComponent to the GameObject with the ContextProviderComponent.

 

Navigation to the GameObject in the Unity Editor. Press the “Add Component” button => Utility AI Client.

Note
The LoadBalancerComponent (also provided with Apex Path) must be present in the scene if using the UtilityAIComponent, since it schedules the AI execution on the load balancer. Without it, the AI will not execute automatically.

Step 4: Create a Utility AI and add this to the UtilityAIComponent

Go to Tools => Apex => AI Editor => Click “New”, enter a name, and press OK 

Or 

Right click in Unity project window, select: Create => Apex => Utility AI => Enter a name and press, OK.

Navigate to the UtilityAIComponent added to the GameObject in Step 3, click the plus (+) mark in the top right corner, and select the Utility AI you just created.

Note that in the UtilityAIComponent you can change the frequency by which the AI executes, as well as the initial delay for the execution of AI.

Congratulations! You have now successfully created an empty Utility AI ready for development and added this to a GameObject. Once you “Play” the scene, the Utility AI will run.