public bool dispatch(Event _event, StateData toState, bool bCheckOnly) { if (m_StateMachineImpl == null) return false; bool bDispatched = false; if(toState == null || !toState.IsActiveState() && !bCheckOnly) return bDispatched; switch ((StateEnum)toState.state_enum) { case StateEnum.DriveDistance_VIRTUAL_SUBMACHINESTATE: if(_event.eventEnum == EventEnum.COMPLETION) { if(!bCheckOnly) { m_StateMachineImpl.ReleaseSubmachineState(toState); List<StateData> lstStateData = m_StateMachineImpl.GetStateData(); foreach (StateData state in lstStateData) { if (state == toState) { lstStateData.Remove(state); break; } } //delete toState; toState = null; } bDispatched = true; } break; case StateEnum.DriveDistance_ENUM_DRIVEDISTANCESM_FORWARDSTATE: switch (_event.eventEnum) { case EventEnum.COMPLETION: if(abs(nMotorEncoder[MOTOR_A]) > STOP_DIST) { if(!bCheckOnly) TransitionProc(TransitionEnum.DriveDistance_ENUM_FORWARDSTATE__TO__STOPSTATE_6, null, toState.submachine_state); bDispatched = true; break; } break; } break; } if (!bDispatched && toState != null && toState.parent_state != null && _event.eventEnum != EventEnum.COMPLETION) { bDispatched = dispatch(_event, toState.parent_state, true); if (bDispatched && !bCheckOnly) { /*1. Exit the current state; 2. Decrement the active count of the parent state; 3. dispatch the event to parent state*/ StateProc((StateEnum)toState.state_enum, toState.submachine_state, StateBehaviorEnum.EXIT, null); toState.parent_state.DecrementActiveCount(); dispatch(_event, toState.parent_state, false); _event = null; } } return bDispatched; }