GOAP
Ask or search…
K
Comment on page

Code

  1. 1.
    Create a new scene
  2. 2.
    Create a new GameObject called Goap, add the GoapRunnerBehaviour to it.
Goap Runner Behaviour
  1. 3.
    Create a class called WanderTarget that extends TargetKeyBase.
WanderTarget.cs
1
using CrashKonijn.Goap.Behaviours;
2
3
public class WanderTarget : TargetKeyBase
4
{
5
}
  1. 4.
    Create a class called IsWandering that extends WorldKeyBase.
IsWandering.cs
1
using CrashKonijn.Goap.Behaviours;
2
3
public class IsWandering : WorldKeyBase
4
{
5
}
  1. 5.
    Create a class called GoapSetConfigFactory that extends GoapSetConfigFactoryBase and override the Create method.
GoapSetConfigFactory.cs
1
using CrashKonijn.Goap.Behaviours;
2
using CrashKonijn.Goap.Classes.Builders;
3
using CrashKonijn.Goap.Configs.Interfaces;
4
using CrashKonijn.Goap.Resolver;
5
using CrashKonijn.Goap.Enums;
6
7
public class GoapSetConfigFactory : GoapSetFactoryBase
8
{
9
public override IGoapSetConfig Create()
10
{
11
var builder = new GoapSetBuilder("GettingStartedSet");
12
13
// Goals
14
builder.AddGoal<WanderGoal>()
15
.AddCondition<IsWandering>(Comparison.GreaterThanOrEqual, 1);
16
17
// Actions
18
builder.AddAction<WanderAction>()
19
.SetTarget<WanderTarget>()
20
.AddEffect<IsWandering>(EffectType.Increase)
21
.SetBaseCost(1)
22
.SetInRange(0.3f);
23
24
// Target Sensors
25
builder.AddTargetSensor<WanderTargetSensor>()
26
.SetTarget<WanderTarget>();
27
28
// World Sensors
29
// This example doesn't have any world sensors. Look in the examples for more information on how to use them.
30
31
return builder.Build();
32
}
33
}
  1. 6.
    Add the GoapSetConfigFactory to the Goap GameObject. Make sure to add the GoapSetConfigFactory to the GoapRunnerBehaviour's factories property.
Goap Runner
  1. 7.
    Create a script called GoapSetBinder. This script will assign a GoapSet to the Agent.
GoapSetBinder.cs
1
using CrashKonijn.Goap.Behaviours;
2
using UnityEngine;
3
4
public class GoapSetBinder : MonoBehaviour {
5
public void Awake() {
6
var runner = FindObjectOfType<GoapRunnerBehaviour>();
7
var agent = GetComponent<AgentBehaviour>();
8
agent.GoapSet = runner.GetGoapSet("GettingStartedSet");
9
}
10
}
  1. 8.
    Create a sphere GameObject called Agent. Add the AgentBehaviour, AgentMoveBehaviour, AgentBrain and GoapSetBinder to the GameObject.
Agent
  1. 9.
    Run the scene. The agent should move around randomly.