Simple AI Question

Hi All,

I use FSM in my game. The main state loop goes like this

void Start()
{
   while (!dead)
   {
     yield return StartCorutine(currentState.Run())
   }
}

As you can see I want to wait untill the state execution is over before continue to the next iteratin in the while loop,
My question is as follows :

If the FSM starts to execute a sate and in the middle of execution some variable of the game object are changed due to state change does it mean the the state that was in the middle of execution sees the new variable of the one it started with?

Thank you in advance
Eyal

OK maybe that will help. Here is what I have done:

var any:boolean;
var once:boolean;
function Start(){
	any=true;
	once=true;
}
function Update(){
	if(once){
		CoroutineA();
		CoroutineB();
		once=!once;
	}
}
function CoroutineA(){
    Debug.Log(any);
    yield WaitForSeconds(1.0f);
	any=!any;
	Debug.Log(any);
}
function CoroutineB(){
	yield WaitForSeconds(2.0f);
	if(any)
		Debug.Log("True");
	else
		Debug.Log("False");
}

And result are True, False, False meaning that CoroutineA waits 1s and flips any. In the meanwhile CoroutineB has been entered and printed True, waits 2s and now any is false.

Now second case, I pass the value to the function, now the value is local and won’t alter the global one if no return, if there is a return we are back to case 1 and depending on when it happens:

var any:boolean;
var once:boolean;
function Start(){
	once=true;
	any=true;
}
function Update(){
	if(once){
		CoroutineA(any);
		CoroutineB(any);
		once=!once;
	}
}
function CoroutineA(any:boolean){
	yield WaitForSeconds(1.0f);
	any=!any;
	Debug.Log(any);
}
function CoroutineB(any:boolean){
	Debug.Log(any);
	yield WaitForSeconds(2.0f);
	if(any)
		Debug.Log("True");
	else
		Debug.Log("False");
}

It prints out true, false, true.

Conclusion, obvisouly, if you use global variable, the function will use its state at the moment not when entered, if you pass it as argument then it is the value at the moment of launching the function. So if you want to keep the value as it was when starting the function pass the value so that you have a local variable to the function that keeps the value until the end.

Helpful?