See all glossary terms

AI (Game AI)

In games, we talk about AI for any entity or agent controlled by the computer and 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.
NOTE:
It's not that game developers don't know how to code really efficient or smart AIs. They intentionally limit AIs for many reasons: budget, performance, time, user experience... For example, a computer-controlled enemy that is very efficient and anticipates all your moves is relatively easy to code but can be unbeatable and feel unfair and frustrating to play against.

Common AI techniques used in games

Game developers employ various AI techniques to create compelling and interactive game experiences. Some of the most common techniques include:
  • If statements (!)
  • Pathfinding algorithms
  • Structural programming patterns like finite state machines, behavior trees, and goal-oriented action planning

If statements

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

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:
  1. The A* algorithm: It's a simple and efficient pathfinding algorithm that works well for grid-based games or when you create your own graph structure, for example, for navigating the world map in a Mario-like game. You can use it to find the shortest path between two points on a grid or a graph.
  2. The navigation server: It's a more complex system that uses navigation meshes to create paths for characters to follow. The Godot editor provides tools to analyze your game levels and generate geometric shapes that represent walkable areas. The navigation server can then use these shapes to calculate paths for characters to follow.
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.

Structural programming patterns

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:
  1. Finite State Machine (FSM): In this pattern, the AI has a set of states and transitions between them. Each state represents a behavior, and the transitions represent the conditions that trigger the state change. It's a staple in indie games and is used in Hollow Knight, for example, and the pattern scales up to really complex games. Id Software used a variant of finite state machines in the FPS games Doom 2016 and Wolfenstein: The New Order. FSMs work great for enemies that should follow patterns and have predictable behavior, though not exclusively.
  2. Goal-Oriented Action Planning (GOAP): In this pattern, the AI has a set of goals and actions that can be performed to achieve them. The AI will choose the best action to achieve its goal based on the current state of the game. It allows the AI to be flexible and adapt to different situations. It's the structure used in the classic FPS game FEAR.
  3. Behavior Trees: In this pattern, you code the AI using small logical nodes that can be composed into a tree to create complex behaviors. Behavior trees are almost like a little programming language for AI. Nodes represent behaviors or conditional branches to sequence or select behaviors. This approach to AI was popularized by Halo 2 and has been used extensively in triple-A games since then. There's a powerful Godot plugin named Beehave that you can use to implement behavior trees in your Godot games.
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.
How do I choose the right pattern for my game?
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.
NOTE:
Unlike the other two patterns, FSMs are not only used for AI but also for player characters and other game systems. They're a way to break down complex systems into manageable pieces. You can learn more here: Finite State Machine.