RodZill4 / godot-goap

Very simple GOAP addon and example for Godot Engine
MIT License
77 stars 16 forks source link

godot-goap

This addon provides a simple Goal Oriented Action Planning implementation for the Godot Engine and an example of how it can be used.

What is GOAP ?

GOAP is an approach to non-playing character AI where:

This approach makes it easy to create complex behaviors (without explicitly defining the corresponding state machine), and add or remove possible actions dynamically.

The addon

The Action Planner is defined in the action_planner.gd, that can be attached to a Node. It provides the following methods:

The syntax for all boolean expressions is simple. Valid characters for variables are letters, digits and underscores. A ! prefix for a variable means that the variable is set to false. All other characters are separators (but space is a good choice for readability).

For example, is the state of the character is !hungry has_axe sees_tree !sees fruit, this means the character is not hungry, is holding an axe, and he sees a tree and no fruit.

When attaching the action planner script to a Node in a scene, actions can be added as children of type Node with the action.gd script, whose exported variables are similar to the parameters of the add_action method. In this case, actions are parsed automatically when the planner is inserted in the scene tree, and the plan can directly be used.

Actions can also be declared programmatically using the clear_actions and add_action functions.

The example

The example is a scene that shows a character, a box, an axe and a few trees. The character can be controlled either directly or by the AI that can be started using the G key.

A small video of the demo

The character can only hold one object at a time: an axe, a piece of wood or a fruit.

The main goal of the AI is to gather wood and stay alive. A secondary goal is to make sure there are always more than 10 trees available (so the character does not just concentrate on gathering wood, but makes sure wood will be available in the future).

The boolean variables that describe the world are:

The available actions are: