AI (Game AI)
2025/12/27
- Type
- Learning Resource
- Format
- Glossary Article
- Version
- General
- Subject Tags
- Created
- Updated
- 2026/02/16
- 2025/12/27
In games, we talk about AI for any entity or agent controlled by the computer that has some sort of decision-making logic. Whether it uses pathfinding algorithms like A\ for navigation, a state machine that checks the current player state to alternate between different behaviors, or makes a choice within a list of actions in a turn-based card game, all of that is part of game AI.
With the modern developments in the artificial intelligence field and the popularity of large language models (LLMs), people started thinking of LLMs as the only thing you might call "AI." However, it's far from being the only form of it.
Game AI and machine learning are distinct fields with different goals. Game AI primarily focuses on creating fun challenges for the player in a game. It generally does not try to make the AI really "smart" in the human sense. It often uses predefined rules, heuristics, and algorithms to provide a balanced challenge to the player tailored to the game world.
On the other hand, general AI and machine learning aspire to create systems that can learn and adapt to new situations, mimic, and eventually match or even beat human intelligence.
While some games incorporate machine learning techniques, most game AI still relies on more traditional and deterministic approaches to ensure the game stays balanced and fun for the player.
Game developers employ various AI techniques to create compelling and interactive game experiences. Some of the most common techniques include:
If statements are the simplest form of decision-making logic in programming. They allow you to check a condition and execute different code based on whether the condition is true or false. In game AI, if statements are often used to determine the behavior of NPCs based on factors like player proximity, health status, or environmental conditions. Some games are entirely built on if statements.
Here's an example of an AI coded with if statements in GDScript. The _physics_process() function is called every frame, and it checks if the player is within a certain range. If the player is in range, the AI chases the player; otherwise, it stays idle:
extends CharacterBody2D
@export var detection_range := 600.0
# In this example, the player node is provided by another script.
var player: Node2D = null
func _physics_process(delta: float) -> void:
var distance_to_player := global_position.distance_to(player.global_position)
var is_player_in_range := distance_to_player < detection_range
if is_player_in_range:
# Chase the player
velocity = global_position.direction_to(player.global_position) * 400.0
else:
velocity = Vector2.ZERO
move_and_slide()Pathfinding algorithms like A* (read A-star) or navigation meshes are used to determine the fastest path for characters to navigate through game environments. These algorithms are essential for AIs to navigate game levels without getting stuck or looking dumb.
Godot offers mainly two complementary options for pathfinding:
Note that these systems are not exclusive to AIs. You can use them to create systems that move the character where the player clicks, for example.
As your game AIs become more complex and you need a greater variety of them in your game, it can make sense to use a more elaborate structure to organize your code, especially if you need teammates who don't know how to code to help you create AIs.
Here are the three most common structures used in games over the past two decades:
Each pattern gives more flexibility than the previous one at the cost of complexity. Finite State Machines are conceptually the simplest, and behavior trees are the most powerful and complex option.
There is no way around experimenting with the patterns in practice to evaluate which is the most suitable for your game. I recommend always starting with the simplest code possible to design your AIs and refactor them as needed. You can run little experiments and make prototypes using different patterns on the side to see which one fits your game best. A great experiment is to have one or two developers code the exact same two or three mobs from your game using two or three different patterns. Then, you can compare the results and see which one is easier to understand and maintain.
If you don't have experience with the patterns and you just want to get going, start with simple code using if statements and when you hit the limits of that, move on to finite state machines.
Don't stop here. Step-by-step tutorials are fun but they only take you so far.
Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.
Get help from peers and pros on GDQuest's Discord server!
20,000 membersJoin ServerThere are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!
Sponsor this library by learning gamedev with us onGDSchool
Learn MoreImprove and build on assets or suggest edits onGithub
Contributeshare this page and talk about GDQUest onRedditYoutubeTwitter…