AI (FSM, Behavior Tree, GOAP, Utility AI)

Nezarrow-up-right includes several different options for setting up AI ranging from a super simple transitionless finite state machine (FSM) to extendable behavior trees to ultra-flexible Utility Based AI. You can mix and match them as you see fit.

The fastest and easiest way to get AI up and running. Simple State Machinearrow-up-right is a Component subclass that lets you set an enum as its generic constraint and it will use that enum to control the state machine. The enum values each map to a state and can have optional enter/tick/exit methods. The naming conventions for these methods are best shown with an example:

enum SomeEnum
{
    Walking,
    Idle
}

public class YourClass : SimpleStateMachine<SomeEnum>()
{
    void OnAddedToEntity()
    {
        initialState = SomeEnum.Idle;
    }

    void Walking_Enter() {}
    void Walking_Tick() {}
    void Walking_Exit() {}

    void Idle_Enter() {}
    void Idle_Tick() {}
    void Idle_Exit() {}
}

The next step up is State Machinearrow-up-right which implements the "states as objects" pattern. State Machinearrow-up-right uses separate classes for each state so it is a better choice for more complex systems.

We start to get into the concept of a context with State Machinearrow-up-right. In coding, the context is just the class used to satisfy a generic constraint. in a List<string> the string would be the context class, the class that the list operates on. With all of the rest of the AI solutions you get to specify the context class. It could be your Enemy class, Player class or a helper object that contains any information relevant to your AI (such as the Player, a list of Enemies, navigation information, etc).

Here is a simple example showing the usage (with the State subclasses omitted for brevity):

The de facto standard for composing AI for the last decade. Behavior trees are composed of a tree of nodes. Nodes can make decisions and perform actions based on the state of the world. Nez includes a BehaviorTreeBuilderarrow-up-right class that provides a fluent API for setting up a behavior treearrow-up-right. The BehaviorTreeBuilderarrow-up-right is a great way to reduce the barrier of entry to using behavior trees and get up and running quickly.

Compositesarrow-up-right are parent nodes in a behavior tree. They house 1 or more children and execute them in different ways.

Conditionalsarrow-up-right are binary success/failure nodes. They are identified by the IConditionalarrow-up-right interface. They check some condition of your game world and either return success or failure. These are inherently game specific so Nezarrow-up-right only provides a single generic Conditional out of the box and a helper Conditional that wraps an Action so you can avoid having to make a separate class for each Conditional.

Decoratorsarrow-up-right are wrapper tasks that have a single child. They can modify the behavior of the child task in various ways such as inverting the result, running it until failure, etc.

Actionsarrow-up-right are the leaf nodes of the behavior treearrow-up-right. This is where stuff happens such as playing an animation, triggering an event, etc.

GOAParrow-up-right differs quite a bit from the other AI solutions. With GOAP, you provide the planner with a list of the actions that the AI can perform, the current world state and the desired world state (goal state). GOAP will then attempt to find a series of actions that will get the AI to the goal state.

GOAP was made popular by the old FPS F.E.A.R. The AI in F.E.A.R. consisted of a GOAP and a state machine with just 3 states: GoTo, Animate, UseSmartObject. Jeff Orkin's web pagearrow-up-right is a treasure trove of great information.

The brains of the operation. You give the ActionPlannerarrow-up-right all of your Actions, the current world state and your goal state and it will give you back the best possible plan to achieve the goal state.

Actions define a list of pre conditions that they require and a list of post conditions that will occur when the Actionarrow-up-right is performed. ActionTarrow-up-right is just a subclass of Actionarrow-up-right with a handy context object of type T.

Agentarrow-up-right is a helper class that encapsulates an AI agent. It keeps a list of available Actions and a reference to the ActionPlannerarrow-up-right. Agentarrow-up-right is abstract and requires you to define the GetWorldState and GetGoalState methods. With those in place getting a plan is as simple as calling agent.Plan().

Utility Theory for games. The most complex of the AI solutions. Best used in very dynamic environments where its scoring system works best. Utility based AIarrow-up-right are more appropriate in situations where there are a large number of potentially competing actions the AI can take such as in a RTS. A great overview of utility AI is available herearrow-up-right.

Selects the best Considerationarrow-up-right from a list of Considerations attached to the Reasonerarrow-up-right. The root of a utility AI.

Houses a list of Appraisalarrow-up-rights and an Actionarrow-up-right. Calculates a score that represents numerically the utility of its Actionarrow-up-right.

One or more Appraisalarrow-up-rights can be added to a Considerationarrow-up-right. They calculate and return a score which is used by the Considerationarrow-up-right.

The action that the AI executes when a specific Consideration is selected.

Last updated

Was this helpful?