# Basic AI finite state machine: where to put decision logic?

I'm trying to convert my enemy AI to an FSM, and am having trouble wrapping my head around where in my script to put the actual decision logic. I built this basic template:

UPDATE: I updated the code to illustrate a solution I came up with this morning, works pretty well so far. Feel free to crib it.

``````var target : Transform;

//wandering vars
public var wanderSpeed = 2.0;
public var wanderRotSpeed = 5.0;
public var wanderRayDistance = 5.0;
public var wanderPauseMin = 2.0;
public var wanderPauseMax = 6.0;
private var basePosition : Vector3;
private var currentDestination : Vector3;

//chase vars
var chaseDistance : float = 10.0;
var chaseSpeed : float = 3.0;
var chaseRotSpeed : float = 5.0;

//attack vars
var attackDistance : float = 3.0;
var attackRate : float = 0.25;

//state setup
enum aiState{ wandering, chasing, attacking }
var state : aiState;

InvokeRepeating("StateLogic", 0.0, 0.05);

function Start(){
if(target == null)
target = GameObject.FindWithTag("Player").transform;
ChooseNextDestination();
yield StateMachine();
}

function StateLogic(){
var distanceToTarget = (target.position - transform.position).sqrMagnitude;
if(distanceToTarget <= attackDistance*attackDistance)
state = aiState.attacking;
else if(distanceToTarget <= chaseDistance*chaseDistance)
state = aiState.chasing;
else
state = aiState.wandering;
}

function StateMachine(){
while(true){
switch(state){
case aiState.wandering:
yield Wander();
break;
case aiState.chasing:
Chase();
break;
case aiState.attacking:
yield Attack();
break;
}
yield;
}
}

function Wander(){
RotateToward(currentDestination, wanderRotSpeed);
MoveForward(wanderSpeed);
var destPosZeroY = currentDestination;
var currentPosZeroY = transform.position;
destPosZeroY.y = 0;
currentPosZeroY.y = 0;
if((destPosZeroY - currentPosZeroY).magnitude < 1.0){
yield WaitForSeconds(Random.Range(wanderPauseMin, wanderPauseMax));
ChooseNextDestination();
}
}

function ChooseNextDestination(){
var randOffset : Vector2 = Random.insideUnitCircle * wanderRadius;
currentDestination = basePosition + new Vector3(randOffset.x, transform.position.y, randOffset.y);
Debug.DrawLine(transform.position, currentDestination, Color.white);
}

function Chase(){
RotateToward(target.position, chaseRotSpeed);
MoveForward(chaseSpeed);
}

function Attack(){
target.GetComponent(PlayerStatus).TakeDamage(20.0);
yield WaitForSeconds(attackRate);
}

function RotateToward(targetPos : Vector3, rotSpeed : float){
targetPos.y = transform.position.y;
var rotation = Quaternion.LookRotation(targetPos - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotSpeed);
}

function MoveForward(moveSpeed : float){
transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
}

``````

But I don't know where to put my actual decision-making logic. Should I just call MakeDecision once in Start, and do a check inside each of the coroutines for my states, making a quick decision to see if the condition that I required to be satisfied for this state is still satisfied, and if not, return?

Is there a way I could separate the decision making logic from the coroutines for each state, so I don't have to pepper every function with checks to make sure whatever condition got me there is still satisfied?

I'm actually not 100% clear on your problem. However, I'm going to offer a suggestion, that hopefully will help you. Unity 3 added function types in JavaScript. That means you can have nice, compact code that switches functions, and you can run the appropriate functions in whatever coroutine you want. Here, I'll make the decision every frame, to decide what to do:

``````enum AIState {Wandering, Chasing, Attacking}
var aiState : AIState;

private var act : function ();

function Update () {
MakeDecision();
act();
}

function MakeDecision () {
switch (aiState) {
case AIState.Wandering :
act = Wander;
break;
case AIState.Chasing :
act = Chase;
break;
case AIState.Attacking :
act = Attack;
break;
}
}

``````

The standard for the enum list is to use PascalCase, not camelCase, by the way. If you use these "function types", aka delegates, you may not need to have the enum in the first place, though. I'd like to hear more about whatever coroutines you're using.

The docs don't mention if you can declare an array of functions in JavaScript, but I doubt it; I can't figure a method for it, anyway. Here's how you might do it in C#:

``````enum AIState {Wandering, Chasing, Attacking}
[SerializeField] AIState aiState;

delegate void Action ();
Action[] actions;

void Awake () {
actions = new Action[]{Wander, Chase, Attack};
}

void Update () {
actions[(int) aiState]();
}

``````

I think this way might be faster, but I'm not sure. I think it's better because it's slightly more clear to me, though:

``````Dictionary<AIState, Action> actions;

void Awake () {
actions = new Dictionary<AIState, Action>() {
{AIState.Wandering, Wander},
{AIState.Chasing, Chase},
{AIState.Attacking, Attack},
};
}

void Update () {
actions[aiState]();
}

``````

I highly recommend switching to C# at this point, given your apparent level of scripting knowledge, and desires.

If your statemachine is selfcontrolled you want to call MakeDecision either in Update() every frame, or in a coroutine loop. But if the transitions should be triggered from outdide you have to call it when a decision is required and that's up to you.