Connect(IState, IState)
Creates a transition from one state to another.
Read time 2 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: Unity.GraphToolkit.Editor
- Assembly: UnityEditor
public ITransition Connect(IState fromState, IState toState)
Parameters
Returns
Type | Description |
|---|---|
| ITransition | The ITransition the connection was made on. If |
Remarks
This is the state machine equivalent of connecting two ports with a wire. Between two different states, a new transition is created on every call, so calling this method twice on the same pair of states produces two distinct transitions, each seeded with a single empty rule.
A state holds a single self transition, so when and are the same state and that state already has a self transition, no second transition is created: a new rule is added to the existing self transition, which is returned. To retrieve the rule that was added, read the last rule of the returned transition. Use ITransition.GetRules to inspect the rules of a transition.
fromStatetoStateEnclose this method with StateMachine.UndoBeginRecordStateMachine and StateMachine.UndoEndRecordStateMachine to add this operation to the undo stack and to update the graph view with the changes. Throws ArgumentNullException when or is . Throws ArgumentException when either state does not belong to this state machine.
fromStatetoStatenullAdditional Resources: StateMachine.GetTransitions, StateMachine.Disconnect
The following example connects two states, then gives the second state a self transition with two rules.
Examples
void BuildTransitions(StateMachine stateMachine, IState idle, IState patrol){ stateMachine.UndoBeginRecordStateMachine("Build transitions"); // Idle -> Patrol, a new transition holding one rule. stateMachine.Connect(idle, patrol); // Patrol -> Patrol, a self transition holding one rule. var patrolLoop = stateMachine.Connect(patrol, patrol); // Patrol already has a self transition, so this adds a second rule to it rather than // creating another transition: it returns patrolLoop, whose RuleCount is now 2. stateMachine.Connect(patrol, patrol); stateMachine.UndoEndRecordStateMachine();}