Create a AI GOAP

I’m trying to create a GOAP AI in the Unity engine, but I’m having trouble figuring out where to start. Please don’t send any video or documentation links as I’ve probably already seen them, and I’m also not interested in using third-party resources that make the AI creation process easier (e.g., plugins). I’m willing to pay for someone’s guidance, so if you know where I can find a qualified person, please let me know.

I was trying out GOAP in the past. I found it had two major issues. The first is poor performance and the other is that it doesn’t handle sequences of events very well e.g. combo attacks. Most of the studios that used GOAP seem to have switched to HTN planners instead. I tried HTN planners and I liked them more than GOAP, the new Unity Behavior graphs is also really good.

2 Likes

If your GOAP implementation is suffering from poor performance, you’ve either made its world state far too encompassing or you evaluate too many skills because you weighted the all in such a way that your decision making system is testing all of them. Either that or you tried to use it for far, far too many entities. My core AI framework for every project is a custom GOAP implementation.

You start at the beginning, cliche as it sounds. GOAP ultimately ends up being an A* implementation with a few extra steps. Remember, A* is about graph traversal before it’s about pathfinding. So what you do is you establish the actions the Actor can take. Let’s use a very simple practical example since the one they give in the FEAR presentation from way back is a little bit abstract.

Actor abilities:

  1. move
  2. open green door
  3. press button to open red door

Now you need the world state. In a large world, you may want some form of culling, but for this example you can assume that the Actor is aware of the entire state of this example world:

To get to the marked destination, there are several options available. The Actor must find the lowest cost set of steps to reach its destination. Let’s assume that walking 1 unit has a cost of 0 just to make things simple, opening a green door has a cost of 2, and pressing the red button has a cost of 1. Simple A* will start looking at the options and see that reaching the destination via the green doors will have a total cost of 6, while pressing the red button will only have a cost of 1. If we start factoring in the movement cost (evaluate the distance travelled based on a specific cost to each potential step), we may find that using the green doors will be the more desirable path.

There are more potential considerations here. For instance, I tend to keep the known world state for an Actor limited to a certain radius or amount of adjacent rooms; I’ll make it so squads of Actors all draw from the same world state to make it seem more like they’re working together; if I need to have an Actor/Actors all go to a specific destination and be aware of a larger world state, I’ll use a governor system create a simplified world state to feed them along a path that will only ever update as needed.

All you need to do to start is define a world state, define actions, and define their costs. From there, it’s honestly pretty simple and the specifics will come down to your own needs.

2 Likes

Your comment was very helpful, thank you. There’s one detail I didn’t make clear: I know how GOAP works in theory. I know there must be a world state for the actor to make the right decisions, there must be a planner that chooses actions based on their cost, actions have prerequisites to be executed, as well as their effects after completion. I’m already familiar with all of that and more.

My difficulty is in writing the code. I don’t know which functions I should use or how to structure the code. For example, before I started studying GOAP, I had no idea what KeyPairValue, Stack, delegate, or script interface were.

Here are some materials I was studying:

1st Material

2nd Material

(I forgot which engine version this project works with. If you figure it out, let me know.)

Do you know how to make a GOAP AI? If so, would you be interested in guiding me? (As I mentioned in my post, I’m willing to pay.)

You can pull these repos as new project each. When launch them for the first time from Unity Hub, it will show you which Unity version it is using.

Perhaps the issue is not the AI GOAP system, but general lack of expertise in core Unity API, which makes it more difficult to understand, how things works.

GOAP systems are well documented and there are many solutions available. And tutorials.

But looks like there is skill difficency, which would be good to improve, before moving to GOAP design.

1 Like

You don’t need to pay somebody for guidance with GOAP in particular, you need to study C# itself. KeyValuePair, stacks, delegates, and interfaces are all beginner level to intermediate level C# features. If you try and implement something like GOAP without knowing the language you’re developing in, you’re naturally going to have a bad time.

1 Like

It seems you still haven’t understood my situation. Learning C# won’t teach me how to build a GOAP AI. If you really work with programming, you should know that programming itself isn’t difficult; the challenge is designing the system architecture. Basically, what you’re telling me is:

“If you know the meaning and utility of functions, you’ll be able to do it,” but the reality is that the system needs to be designed in a way that doesn’t conflict with other systems and is easy to understand and maintain.

Another example: I can use a Delegate in one way to build a character movement system, but I can also use a Delegate in a different way to build a GOAP AI.

I’ve been studying GOAP AI for almost two months now, but I still can’t write any code because I haven’t seen an example simple enough for me to absorb.

You don’t seem to understand that if you don’t understand the fundamental rules that govern how a programming language works, you won’t be able to make anything. A KeyValuePair is one of the first things you’ll learn about when dealing with collections in C#, for instance. If you understand the theory and you understand the language, you can implement any number of things.

Yeah, I’d agree with Murgilod that a solid programming foundation will help you build most systems. The overview provided earlier should be a good starting point. There are plenty of detailed tutorials about A* that you could use. If you’re more concerned about software architecture then I suggest checking out design patterns (GoF) or maybe SOLID principles. Although it’s probably not worth worrying too much about architecture at this point.

Without fundamental C# and knowledge/experience with programming patterns you are going to struggle to build something on your own.

https://assetstore.unity.com/packages/tools/behavior-ai/goap-252687

This is a good starting point. Free, discord, many users, responsive dev, third party youtube videos helping you learn how to implement and use his asset and more.

Hey there,

I’m the creator of this GOAP.

I’ve seen a couple post from you about learning GOAP by now and I’m going to try and give you some advice.

People are trying to help you, but you’ve been pretty rude in response to them so far. In your latest post you’re even more demanding, but people don’t owe you anything. Just as a general advice; if you want things from people you should be nice to them. Fully capitalized sentences, internet screaming, is a no-go if you want to achieve something.

Understanding how programming works isn’t hard, everyone can understand what if statements do. But writing (good) software is very hard! Being good at programming means that you can receive a problem, chunk it down into manageable parts and then actually build it. The difference between a junior and a senior programmer is mostly that, how large a problem you can give someone without drowning.

If you’ve read Jeff Orkin’s paper and don’t understand where to start, that’s a programming skill issue. Hence why people are telling you to get better at it. There’s absolutely no shame in not understanding something and people are always willing to help (as long as you’re nice about it!).

The GOAP algorithm in itself actually isn’t that hard, you can write it in ~200 lines of code. The other 20k lines of code are there to actually make it usable in unity. I was going to give you an example of these 200 lines, but I’ll do this instead:

In GOAP you configure goals with conditions, and actions with conditions and effects. To have a functioning algorithm that can return the best action you need to write 2 algorithms:

  • A GraphBuilder. This is a class that you can give your configuration of goals and actions. Based on this it creates a node based graph and makes connections between a condition (of a goal or action) to an effect (of an action).
  • A GraphResolver. You give this class the graph, the entry node (the goal it needs to reach) and the world state. This class should use a modified version of the A* algorithm to determine what the cheapest action is.

You should’ve been able to deduce this from Jeff Orkin’s paper, that’s what everyone that’s ever build a GOAP system did. Building this algorithm isn’t even the hard part, designing a functioning AI around it is way harder! There’s absolutely no shame in not being able to do that, you just need to gradually build your experience with building software.

It took me 20 years of programming to get where I am now. There’s no shortcuts in learning to write software, it simply takes (a lot of) time and projects to get there. You’ve stated in another post that you can make “a copy of Super Mario World”, great, now go build something that’s slightly more complex!

If you really want to be able to (learn to) build games you need other people’s help. You can only do that if you’re humble towards people that take the time to help you. You’ve clearly figured out that you don’t understand how GOAP works, and multiple people have told you why that is. I think you should become aware of this graph. The way you talk about programming makes it pretty clear that you’re on the left peak. I think being aware of this might help you.

If you have any questions don’t hesitate to ask. Just be nice, especially to people actually responding.

4 Likes

I recognize that I may have expressed myself rudely in my posts. I know that, despite saying how ignorant I am about programming, I could still create something slightly more complex. Even though I know some functions, I still don’t know how to fully utilize them to achieve my goals.

Here’s an example: in my last post, I asked for help to create an object detection system and then convert the detected object’s position into rotation degrees (perhaps I didn’t provide enough information, which didn’t help). Whenever I’ve asked for help to build code, people often respond by immediately sending links to documentation or YouTube videos (which are often just theoretical content, though I understand these resources can be useful). It feels like the person responding assumes that the person who made the post isn’t capable of finding this information on their own.

For instance, one of the solutions for the code in my last post involved using a list of “Colliders,” adding or removing colliders identified via OnTriggerEnter & OnTriggerExit, and then using Vector3.Distance to determine which collider was closest (that’s the summary). I knew what a list was, and I suspected it could be used in the code, but I didn’t know how to implement it. I managed to figure it out because the same explanation I wrote in my post was interpreted by an AI, which genuinely helped me instead of just sending me a generic YouTube link. The AI suggested using a list and explained how to implement it in the code.

That’s the kind of objective help I’m looking for. In my opinion, the best way to learn programming is through practice. I feel I can learn more efficiently if I see the code (as long as it’s something I can understand) in action, allowing me to replicate it and later try to create something entirely from scratch.

I appreciated your comments and will remember your help.

For people who are just starting out, the theoretical is the most useful because it’s going to teach you how to use these things and how they work with other things. It is about learning how to use the tools you have.

1 Like