- Home /
FSM and coroutine problem
Hi,
I am coding a Javascript finite state machine based on and adapted from the C# FSM framework found at the unity wiki. I am trying to add a delay so the animation ends before changing to another state but I obtain the next error:
Assets/Scripts/example.js(192,18 ) BCE0101: Return type 'void' cannot be used on a generator. Did you mean 'IEnumerator'? You can also use 'System.Collections.IEnumerable' or 'object'.
The script es called example and it has all the state class implementations (Idle, Run, Turnaround,...)
Any ideas?
Thanks
class Turnaround extends FSMState {
function Turnaround() {
stateId = StateId.turnaround;
}
function Reason(player : GameObject) {
yield player.GetComponent(example).StartCoroutine(player.GetComponent(example).wait(1));
player.GetComponent(example).SetTransition(Transition.turnaroundIdle);
}
function Action(player : GameObject) {
player.GetComponent(example).aniSprite(16, 6, 0, 5, 5, 16);
}
}
function wait(waitTime : float) {
yield WaitForSeconds(waitTime);
}
Answer by Dave-Carlile · Oct 30, 2013 at 06:59 PM
This looks related:
http://answers.unity3d.com/questions/58242/yield-waitforseconds-causing-compiler-errors.html
Not sure of the why, but it seems that your wait function needs to explicitely return an IEnumerable type, possibly due to it being in a subclass.
So declare your wait function like this:
function wait(waitTime : float) : IEnumerable
{
yield WaitForSeconds(waitTime);
}
Hi Dave, thank you for the interest. I found the related post, but when I declare the return type IEnumerator in wait() and Reason() functions (also in the parent class function) the error disappears but coroutine is never executed!.
I think it is being executing but you're expecting it to do something after the wait, which isn't specified in that wait method.
Remember that starting a Coroutine is not a "blocking" function. After you start it, the code continues to execute at the next line.
Right now, you wait for 1 second and then ... nothing else happens because nothing else is in the routine.
Answer by Zaqnafein · Oct 31, 2013 at 12:58 AM
Hi Dracorat, I want to invoke the coroutine wait() and wait until it is completed. After that, I want to execute the next line: (`player.GetComponent(example).SetTransition(Transition.turnaroundIdle`).
Something like the second example of the Script Reference link text. I have add Debug.log("in wait coroutine")
in the wait coroutine and the message is never printed...
Thanks in advance