How can I make enemy patrol in godot 4.3 using state machines?

Godot Version

godot 4.3

for example
if it detects player the enemy will break his path and follow player and if he lost his sight of player he’ll return to his old path

  • how can I use pathfinding with state machine ? so I can change the path later on without worrying on the entire code

Probably some states like these?

  • Patrol
    follow path or whatever, check if player is detected
    on exiting from this state you could save the current (global)position for reference, so that you can return there
  • HuntPlayer
    follow player, check every once if it’s detected (== Timer with Autostart, connected to a check function). Maybe when player is not seen anymore, set a timer on which timeout change state to ReturnToPatrol, or create another state in which you search for player
  • ReturnToPatrol
    player is not detected anymore, return to last position of patrolling, and change state to Patrol

About pathfinding, simplest way is using adding a NavigationAgent as child of the enemy.
I’m still learning too, so there’s lot of people who know better than me how to do that. At this moment I need some specific things from pathfinding and I’ll explain what I’m doing, but you probably don’t need that much control:
you can get the navigation map from the NavigationRegion (that you surely have, and baked) with its function get_navigation_map().
Obtained the navigation map, you can compute the path with NavigationServer3D.map_get_path(navigation_map, from, to, true), where “from” and “to” are starting and target positions.
In enemy’s script, you can use the path in the _physics_process to move the enemy.
You can handle the path as a list of points to iterate, just start from second one. Compute the current direction (where to move) as
(path[index] - global_position).normalized()
and move along this direction with
velocity.move_toward(direction * speed, acceleration * delta)
At the start of _physics_process, check if index >= len(path) (maybe emit a signal to end pathfinding, it depends on what you want to do)
At the end of _physics_process check if
(path[index] - global_position) < some_distance_threshold
and if it’s true, add 1 to the index

1 Like

alright thanks

what should I write for the patrol state machine ? because I have no idea of what should I write

You really have lot of freedom here, it really depends on what you want to do. Since we’re already using pathfinding, one of many things you could do is adding some markers in the map; these markers represent the spots the enemy could want to check. Enemy should have access to the list of these markers’ positions. You could:

  1. sample one random marker
  2. go there with pathfinding
  3. wait a bit there
  4. resample another (different) marker and restart the cycle
    During the entire cycle you always check for player (Area2D/3D?)
    Also, with this behaviour I think you don’t really need the ReturnToPatrol state, I think you could actually return to Patrol state directly

I got a question about pathmaking

how can I make a specific node go to a path while being outside the node ?

This video helps explain state machines in a short concise manner, while also including an example of exactly what you’re asking for.

1 Like