- Home /
Question by
Kielgasten · Jan 29, 2013 at 12:31 PM ·
loopswitchfsm
Broken FSM - It only runs once, ignores switch
Hello, I have been trying to make an FSM based on BergZergArcades youtube tutorial which is all well and good, except I cannot get mine to work.
This is my script:
using UnityEngine;
using System.Collections;
public class States : MonoBehaviour
{
private State _state;
private WalkingStates _walkingStates;
// Use this for initialization
IEnumerator Start ()
{
_state = State.Init;
_walkingStates = WalkingStates.Walk;
while (true)
{
switch (_state)
{
case State.Init:
Init();
break;
case State.Idle:
Idle();
break;
case State.Walking:
Walking();
break;
}
yield return 0;
}
}
private void Init()
{
Debug.Log("this is the init method");
_state = State.Idle;
}
private void Idle()
{
Debug.Log("This is the idle method");
_state = State.Walking;
}
private void Walking()
{
Debug.Log("this is the WALKING method");
switch (_walkingStates)
{
case WalkingStates.Walk:
Walk();
break;
case WalkingStates.Circles:
Circles();
break;
case WalkingStates.Stop:
Stop();
break;
}
}
private void Walk()
{
Debug.Log("this is the Walk method");
_walkingStates = WalkingStates.Circles;
}
private void Circles()
{
Debug.Log("this is the Circles method");
_walkingStates = WalkingStates.Stop;
}
private void Stop()
{
Debug.Log("this is the Stop method");
_walkingStates = WalkingStates.Walk;
_state = State.Idle;
}
public enum State
{
Init,
Idle,
Walking
}
public enum WalkingStates
{
Walk,
Circles,
Stop
}
}
It seems that the switch in "Walking()" is being ignored altogether, and the methods just called in sequence. Also, the main loop should be called after each iteration on the Walking() FSM, but it´s not, and I just don´t get it. If someone could help me out, that would be much appreciated. Best Regards Ronnie
Comment
Best Answer
Answer by Kielgasten · Feb 07, 2013 at 05:22 PM
ahem... this was actually working fine. I had turned "collapse" on in the console, giving it the appearance of malfunctioning. In short, nothing to see here. Apologies for wasting internet space. /shame