State Tree initial substates from BP

In State Tree, is there a way to specify the initial substate of a given state based on BP logic? That is, in a State Tree Task’s EnterState event, can it specify which of its state’s substates should be initially selected?

Hi Jeff,

Unfortunately, you cannot use the EnterState event to specify which substate should be selected, because when EnterState is called, state selection has already taken place. Remember that, at the start of a StateTree’s execution and on each state transition that gets triggered, the StateTree will first select the next state, and only then it will deactivate old states and activate new states as appropriate, so that a chain of states from the root to the newly selected state becomes active. This activation/deactivation of states is what calls EnterState() and ExitState() on tasks belonging to the affected states.

Note that state selection will always start at the root or at the “Transition To” state requested by a triggered transition, and make its way down the state hierarchy following the “Selection Behavior” rule of each state along the way and evaluating the “Enter Conditions” of each child state. If you need to “specify the initial substate of a given state based on BP logic”, you can use BP to implement StateTree Conditions and/or Considerations and use them to control the entrance to each substate separately.

Having said all that, I can think of a way to achieve a behavior similar to what you described. First, you can force the StateTree to select the parent “selector” state despite it having children. Then, on its Task’s EnterState event, you can calculate one or more variables (maybe an enum) that will drive the child selection, and call FinishTask(). Finally, you can add “OnStateSucceeded” transitions to each child state directly, each one with its own Trigger Condition that depends on the variables calculated on EnterState. See screenshots below.

[Image Removed]I hope this is helpful. Please let me know if this answers your question and whether the solution above works for you.

Best regards,

Vitor

I see, that mostly makes sense. One thing that is surprising to me is the idea that the superstate’s task finishes the state, but the state doesn’t exit. Instead the State Tree enters the substate, with, I suppose, the superstate still remaining active?

I think a similar approach which would be easier for me to understand would be to have a sibling state do this instead of the super state. That is, have an “Initial” substate, which sets the appropriate params, then completes itself to trigger a transition to one of the other substates.

Hmmm, you are right, my initially proposed approach is indeed a little odd because it relies on the superstate requesting a transition to its own substate upon completion, keeping it in the active path. I agree that a sibling “selector” state coming before the superstate with children is easier to understand and probably a more well-defined behavior as well. Did you have a chance to test this approach and verify that it suits your needs? Let me know if you have any issues.

All the best,

Vitor

Yeah, that will work for us.