So I am working on an AI for my game using GOAP. I've been researching it for the past few days and while implementing it on my own I've ran into a problem. Here is a diagram of what I am doing.

enter image description here

  • The square is the goal and the circles are the actions.

Currently, I am following a path similar to what is shown in the GDC presentation. In the presenter's demonstration, a melee weapon was already equipped. So when deciding child actions for Melee Attack, it will attempt to add on for each precondition. This causes a incorrect path to be created in my case. The red highlighted path is the path it should hypothetically take with the other being incorrect.

Any ideas how I could make it choose the Goto Player first or make it so the left-most path is invalid?

I've thought about doing it by priority but both paths would end up having the same priority.

  • \$\begingroup\$ GOAP always tends to choose the path with the least cost. If you goto the player without a knife, the next step is to find a knife before goto the player again, which consumes more. \$\endgroup\$
    – Mangata
    Commented May 18, 2022 at 7:11
  • \$\begingroup\$ @Mangata I see what you are saying. However, how does it know a path has been created? I just assumed a valid path (not the best path) is when all requirements have been met. So in my example, the issue path would technically be a valid path and wouldn't be able to add a go to correct? This means both paths would have the same cost. Sorry if I am making this more confusing \$\endgroup\$ Commented May 18, 2022 at 17:55
  • \$\begingroup\$ There is an implicit message in your graph: after finding the knife, the player does not have to walk to the target again.(This results in the same cost for each path) This is true if you replace "finding the knife" with "taking out the knife". Maybe you should find the position of the knife on the map and use this to calculate the cost of each action. \$\endgroup\$
    – Mangata
    Commented May 18, 2022 at 19:12
  • \$\begingroup\$ If you don't want to introduce extra complexity, The s of find knife should be has knife; !at player \$\endgroup\$
    – Mangata
    Commented May 18, 2022 at 19:23

1 Answer 1


Assuming you have a class for nodes (which contains parent and children) and that you are working backwards from the action that satisfies the overall goal,then you can artificially add a precondition to the node class

Order preconditions so that one has to be solved first

Then once you find an action that solves the first precondition move the other preconditions into that action's node

for example:

O: Melee Attack -----> at player, has knife(remove from melee attack node after finding go to player action) | O: Go To Player -----> has knife (added artificially) | O: Get knife -----> No preconditions

That way the AI will never consider paths that get the knife AFTER going to the player.

This can help prevent excessive branching too if you have a lot of actions, good for performance

The downside to this is that the AI can get "set in its ways" and become more predictable so only do this for actions a certain depth into the graph


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .