// Note on transition timing: The outgoing transition will trigger on the update() after the task completes. It may // be desired that the transition is triggered 'manually' as soon as the task completes. public static ITransitionBuilder BindAsyncLamdaAndTransitionOnDone <T>(this WithTokenStateConstruction <T> state, Func <T, UniTask> task) { // If the state is entered then exited then entered again before the first UniTask completes, // we need a way to have the first task not cause a transition. Use a simple counter. uint execCounter = 0; bool transitionShouldTrigger = false; state.AddEntryAction(async() => { transitionShouldTrigger = false; execCounter++; uint thisTasksCounter = execCounter; await task(state.ToEntranceType().token); if (thisTasksCounter == execCounter) { transitionShouldTrigger = true; } }); var t = new MultiTransition(state.Graph, state.ToRuntimeState()); state.AddTransition(t); t.When(() => transitionShouldTrigger); return(t); }
public FluentBuilder <T> State <T>(string name) { WithTokenStateConstruction <T> newState = m_graph.NewState <T>(name); m_stateInContext = newState; if (m_states.ContainsKey(name)) { Debug.LogError("Hapn Fluent Builder: multiple states declared with same name."); } m_states[name] = newState; if (m_waitingTransitions.ContainsKey(name)) { foreach (IBaseTransitionBuilder t in m_waitingTransitions[name]) { if (t is ITransitionBuilder <T> typedT) { typedT.To(newState); } else { Debug.LogErrorFormat("FluentBuilder: State '{0}' that requires token created, but transitions-without-tokens were awaiting a state with this name.", name); } } m_waitingTransitions.Remove(name); } LinkWaitingThingsToNewState(name, newState); foreach (var t in m_transitionsToNextState) { var castTransition = t as ITransitionBuilder <T>; if (t == null) { throw new InvalidOperationException("A transition was waiting to be connected to a state with a different token type (or no token) compared to the new state"); } castTransition.To(newState); } m_transitionsToNextState.Clear(); return(new FluentBuilder <T>(this, newState)); }