> For the complete documentation index, see [llms.txt](https://docs.twgamesdev.com/uhfps/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.twgamesdev.com/uhfps/guides/state-machines/adding-ai-states.md).

# Adding AI States

{% hint style="info" %}
Adding new AI States is similar to adding states for the player. Therefore, this guide will only highlight the differences between player states and AI states.&#x20;
{% endhint %}

* The primary distinction lies in the creation of a new state asset. For AI States, select **Create -> C# UHFPS Templates -> AIState Script** to generate a new AI State script.
* The AI state implements the **FSMAIState** class, which includes various methods and references.
* From the **AIState**, you can access essential player components, like the player state machine, player health or player manager.

{% hint style="warning" %}
It is recommended to examine the existing states and methods in **FSMAIState** to get an overview of how the different functionalities are implemented and what you can use in your states. Each method is commented so you will know how and which method you can use.
{% endhint %}

### Adding Waypoints

1. Create a new empty game object and name it **Waypoints** or whatever you want.
2. Add the **AI Waypoints Group** component to the game object.
3. Create a new empty game object and add it to the parent of the **Waypoints** object.
4. Add the **AI Waypoint** component to the parent object. When you add new objects with the **AI Waypoint** component to the parent of the **Waypoints** object, the **AI Waypoint Group** will automatically handle the waypoint list, so you don't need to manually add or remove them.

{% hint style="info" %}
There's no need to assign the waypoints to the **NPC State Machine**. To utilize the waypoints, simply call the <mark style="color:blue;">**FindClosestWaypointsGroup()**</mark> method from the **FSMAIState** class to locate the nearest waypoints based on the NPC's position. You can call this method when you enter a state or whenever you want.
{% endhint %}

{% hint style="info" %}
To obtain an unreserved waypoint from the **AI Waypoints Group**, just use the <mark style="color:blue;">**GetFreeWaypoints()**</mark> method from F**SMAIState**. It's a good to reserve the waypoint once you've acquired it, so that when multiple NPCs are present, they won't head towards the already reserved waypoints.
{% endhint %}

{% hint style="info" %}
To reserve a waypoint, assign the NPC object to the **ReservedBy** object within the **AI Waypoint** component. To unreserve it, simply change that reference to null.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.twgamesdev.com/uhfps/guides/state-machines/adding-ai-states.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
