Comment on page
Through Code
Setting up your GOAP system using code is the most flexible way to configure your GOAP system. This method is more difficult to use than the
ScriptableObjects
method, but allows for a much more dynamic setup.Info By using code to setup your GOAP system, you can use generic classes. This can make the setup of your GOAP system more flexible.
Example The complex demo uses code as the configuration method.
To create a set, you must create a class that inherits from
GoapSetFactoryBase
. This class must implement the Create
method which returns a IGoapSetConfig
. To make building the set easier, you can use the GoapSetBuilder
class.GoapSetConfigFactory.cs
1
using CrashKonijn.Goap.Behaviours;
2
using CrashKonijn.Goap.Classes.Builders;
3
using CrashKonijn.Goap.Configs.Interfaces;
4
using Demos.Complex.Classes;
5
using Demos.Complex.Classes.Items;
6
using Demos.Complex.Factories.Extensions;
7
using Demos.Complex.Interfaces;
8
using Demos.Shared;
9
10
public class GoapSetConfigFactory : GoapSetFactoryBase
11
{
12
public override IGoapSetConfig Create()
13
{
14
var builder = new GoapSetBuilder("ComplexSet");
15
16
// Goals
17
builder.AddGoal<WanderGoal>()
18
.AddCondition<IsWandering>(Comparison.GreaterThanOrEqual, 1);
19
20
builder.AddGoal<FixHungerGoal>()
21
.AddCondition<IsHungry>(Comparison.SmallerThanOrEqual, 0);
22
23
// Actions
24
builder.AddAction<WanderAction>()
25
.SetTarget<WanderTarget>()
26
.AddEffect<IsWandering>(true)
27
.SetBaseCost(1f)
28
.SetInRange(0.3f);
29
30
builder.AddAction<PickupItemAction<IEatable>>()
31
.SetTarget<ClosestTarget<IEatable>>()
32
.AddEffect<IsHolding<IEatable>>(true)
33
.AddCondition<IsInWorld<IEatable>>(Comparison.GreaterThanOrEqual, 1)
34
.SetBaseCost(1f)
35
.SetInRange(0.3f);
36
37
// Target Sensors
38
builder.AddTargetSensor<WanderTargetSensor>()
39
.SetTarget<WanderTarget>();
40
41
builder.AddTargetSensor<ClosestItemSensor<IEatable>>()
42
.SetTarget<ClosestTarget<IEatable>>();
43
44
// World Sensors
45
builder.AddWorldSensor<IsHoldingSensor<IEatable>>()
46
.SetKey<IsHolding<IEatable>>());
47
48
builder.AddWorldSensor<IsInWorldSensor<IEatable>>()
49
.SetKey<IsInWorld<IEatable>>();
50
51
return builder.Build();
52
}
53
}
Add the created class to a GameObject in the scene. Add it to the list on the
GoapRunnerBehaviour
component. This will initialize the set.
Goap Runner Behaviour component
Using a script, set the
GoapSet
property on an agent.1
var goapRunner = FindObjectOfType<GoapRunnerBehaviour>();
2
var set = goapRunner.GetSet("ComplexSet");
3
4
agent.GetComponent<AgentBehaviour>.GoapSet = set;
Last modified 5mo ago