Code

  1. Create a new scene

  2. Create a new GameObject called Goap, add the GoapRunnerBehaviour to it.

  1. Create a class called WanderTarget that extends TargetKeyBase.

WanderTarget.cs
using CrashKonijn.Goap.Behaviours;

public class WanderTarget : TargetKeyBase
{
}
  1. Create a class called IsWandering that extends WorldKeyBase.

IsWandering.cs
using CrashKonijn.Goap.Behaviours;

public class IsWandering : WorldKeyBase
{
}
  1. Create a class called GoapSetConfigFactory that extends GoapSetConfigFactoryBase and override the Create method.

GoapSetConfigFactory.cs
using CrashKonijn.Goap.Behaviours;
using CrashKonijn.Goap.Classes.Builders;
using CrashKonijn.Goap.Configs.Interfaces;
using CrashKonijn.Goap.Resolver;
using CrashKonijn.Goap.Enums;

public class GoapSetConfigFactory : GoapSetFactoryBase
{
    public override IGoapSetConfig Create()
    {
        var builder = new GoapSetBuilder("GettingStartedSet");
        
        // Goals
        builder.AddGoal<WanderGoal>()
            .AddCondition<IsWandering>(Comparison.GreaterThanOrEqual, 1);

        // Actions
        builder.AddAction<WanderAction>()
            .SetTarget<WanderTarget>()
            .AddEffect<IsWandering>(EffectType.Increase)
            .SetBaseCost(1)
            .SetInRange(0.3f);

        // Target Sensors
        builder.AddTargetSensor<WanderTargetSensor>()
            .SetTarget<WanderTarget>();

        // World Sensors
        // This example doesn't have any world sensors. Look in the examples for more information on how to use them.

        return builder.Build();
    }
}
  1. Add the GoapSetConfigFactory to the Goap GameObject. Make sure to add the GoapSetConfigFactory to the GoapRunnerBehaviour's factories property.

  1. Create a script called GoapSetBinder. This script will assign a GoapSet to the Agent.

GoapSetBinder.cs
using CrashKonijn.Goap.Behaviours;
using UnityEngine;

public class GoapSetBinder : MonoBehaviour {
    public void Awake() {
        var runner = FindObjectOfType<GoapRunnerBehaviour>();
        var agent = GetComponent<AgentBehaviour>();
        agent.GoapSet = runner.GetGoapSet("GettingStartedSet");
    }
}
  1. Create a sphere GameObject called Agent. Add the AgentBehaviour, AgentMoveBehaviour, AgentBrain and GoapSetBinder to the GameObject.

  1. Run the scene. The agent should move around randomly.

Last updated