-
Notifications
You must be signed in to change notification settings - Fork 23
Home
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
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
You can create entry node to provide data to your utility AI, for this you can inherit from SimpleEntryNode and override the ValueProvider method :
public class CurrentHpCountNode : SimpleEntryNode {
protected override int ValueProvider(AbstractAIComponent context) {
CubeAIComponent cubeAiComponent = (CubeAIComponent) context;
return cubeAiComponent.CubeEntity.CurrentHp;
}
}
When your SimpleEntryNode script is created you can instantiate the node using the xNode contextual menu in the graph of you AI Brain.
Connect the Value output to one of the X input of an utility function node.
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 class HealNode : SimpleActionNode {
public override void Execute(AbstractAIComponent context, AIData aiData) {
CubeAIComponent cubeAiComponent = (CubeAIComponent) context;
cubeAiComponent.CubeEntity.Heal();
}
}
When your SimpleActionNode script is created you can instantiate the node using the xNode contextual menu in the graph of you AI Brain.
Connect the Linked Option output to the Actions input of an option node, then connect the Utility Y output of your utility function node to the Utilities input of the same option node.
This node translate every values provided to the input into an utility value (an int between 0 and 1).
The Min X and Max X inputs gives the minimum and maximum values of x, X input is the more important data which is calculate into an utility value using the function curve and provided by the Utility Y output.
Of course you can use fixed values using the Min X, Max X, or X inputfields.
You can also choose the function curve to use in order to obtain the utility value you want, for that you only have to click on the function curve image.
And click on the desired curve.
For more convenience, an array of curve is provided, use the gear wheel to access it.
An example is provided with the xNodeUtilityAI framework, it is a minimalist AI but that should help you understand how it works.
Launch the CubeAI scene to access it.