Skip to content
FBast edited this page Jun 13, 2019 · 45 revisions

How to start using xNodeUtilityAI

  • Firstly, download the last release and import it into your Unity Project.
  • Create a script MyAIBrain inheriting from AbstractAIBrain.
  • Create a script MyAIComponent inheriting from AbstractAIComponent and add it to your GameObject.
  • Create scripts inheriting from SimpleActionNode, SimpleEntryNode or CollectionEntryNode.
  • Create a MyAIBrain using Unity ScriptableObject contextual menu.
  • Add your custom nodes on the Node graph with UtilityNode and OptionNode.
  • ...
  • Make profit !

AI Brain

The AI Brain allow you to create a graph to link into your AI Component.

    [Serializable, CreateAssetMenu(fileName = "CubeAIBrain", menuName = "AI/CubeAIBrain")]
    public class CubeAIBrain : AbstractAIBrain {}

When your AI Brain script is created just use the Unity contextual menu to instantiate it Double click on the new file and you will access to the xNode graph window


AI Component

The AI Component allow you to transfer contextual information to your Entry and Action nodes. Moreover it allow you to retrieve the best choosen option using the ChooseOption() method and to execute the linked action with the ExecuteActions(this) method.

    public class CubeAIComponent : AbstractAIComponent {
        
        // Your custom references here
        public CubeEntity CubeEntity;
        // End of custom references

        private void Start() {
            InvokeRepeating("ThinkAndAct", 0, 0.1f);
        }

        private void ThinkAndAct() {
            AIOption option = ChooseOption();
            if (option == null) return;
            option.ExecuteActions(this);
        }
        
    }

Add the component to any GameObject (prefab or in scene) associated to this AI (an enemy for exemple).

Finally, add your AI Brain to the Utility Ai Brain of your component


Simple Entry Nodes

You can create entry node to provide data to your utility AI, for this you can inherit from SimpleEntryNode and override the ValueProvider method :

        protected override int ValueProvider(AbstractAIComponent context) {
            MyAIComponent myAiComponent= (MyAIComponent) context;
            return myAiComponent.CubeEntity.CurrentHp;
        }

Simple Action Nodes

You can create action node to execute action based on your context, for this you can inherit from ActionEntryNode and override the Execute method :

        public override void Execute(AbstractAIComponent context, AIData aiData) {
            MyAIComponent myAiComponent= (MyAIComponent) context;
            myAiComponent.CubeEntity.Heal();
        }

More will follow on how to create custom nodes...

Thanks to

Clone this wiki locally