playMaker

Author Topic: Fully Nestable SubFSM Communication  (Read 494 times)

HardLight680

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Fully Nestable SubFSM Communication
« on: May 07, 2018, 02:03:48 PM »
I made up a small action that allows you to run FSM templates and return data back to the caller regardless of whether the caller is a sub FSM or a host FSM. It is entirely self-contained, so there is no need to use additional actions besides the one RunFSMFunc action. There is no need for any special actions inside the sub FSM either.



The operating principles are similar to the RunFSM action. Variables with the Inspector attribute will appear as inputs, and the sub FSM may terminate using the FinishFSM action.

As a new addition, variables in the template that are inside the 'Output' category will appear as outputs which can be captured into variables for the calling FSM. Output variables are automatically copied by value up into the calling FSM's capture variables when the sub FSM ends without the need for any special actions.

Additionally, string variables in the template that are inside the 'Events' category will generate event selection fields on the action. The template FSM can use those string variables in SendFSMByName with the HostFSM target type in order to send caller specified events back to the caller.

Note that if the calling state ends while a sub FSM is still running, the sub FSM is immediately ended and its current output values are copied to the calling FSM. As a result, if a sub FSM output event causes a state transition in the caller, the sub FSM ends and its current computed values are copied out.

The package contains samples for Factorial and MergeSort FSM templates to show the basics of how it is used, and to show how this can be used for recursive FSM programming.

2018/05/08: Updated to support FSM event outputs.


* PlaymakerRunFsmFunc.unitypackage
« Last Edit: May 08, 2018, 11:11:34 PM by HardLight680 »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3315
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: Fully Nestable SubFSM Communication
« Reply #1 on: May 07, 2018, 02:48:19 PM »
Very cool! This is very similar to an approach I've been playing with to improve the RunFSM action. I love the use of a category to define the outputs - very clever! Thanks for sharing - will give it a go :)

I'd also like sub FSMs to be able to send events to the parent FSM. So a template really can work just like an action. But have been struggling with a design for this - do you have any ideas :)

HardLight680

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Re: Fully Nestable SubFSM Communication
« Reply #2 on: May 07, 2018, 09:22:29 PM »
I never really thought about doing events, but a straightforward solution might be to expose a category of string variables - such as 'Events' - and use those to specify the caller's preferred event triggers.

You would need to use some inspector magic to make sure that the exposed event controls are interpreted as FSMEvent variables instead of FSMString variables, but still get translated properly as string names. Inside the template itself, you could then use SendEventByName with the HostFSM target to send these events back up to the caller.

It looks like specifying HostFSM as the target will send the event up to the most immediate caller, so you don't need to worry about accidentally skipping levels or anything. It also bypasses the need for the events to be marked as Global, so this should work pretty well for keeping things self contained.

The only other thing that might be nice to have would be a FinishFSM variant that allows you to send an event up to the caller and end the FSM at the same time.

Most of this seems pretty straightforward, so I'll see if I can add it to this action.

Edit:

I have updated the package to include the above feature. I did not add the FinishFSM variant as causing a state transition in the caller now automatically causes the sub FSM to terminate. However, since the caller is not guaranteed to respond to events sent from the sub FSM, there may still be certain edge cases where an action like this would be convenient.

So far, it seems relatively stable. There might be a minor issue with cluttering up your string variables with event holders, but it shouldn't be too bad as long as you don't go crazy.

There is also a minor issue in that adding a SendEventByName action to a sub FSM while you are in the nested FSM editor window will cause an exception in the UI. This can be avoided fairly easily if you modify the template directly by clicking on the asset as opposed to clicking on the Edit button inside the action.
« Last Edit: May 08, 2018, 01:38:48 AM by HardLight680 »

craigz

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Haven Made
Re: Fully Nestable SubFSM Communication
« Reply #3 on: August 15, 2018, 07:56:29 PM »
This is incredible! :D

Just started using it and its been super stable. Really a great iteration on the Run FSM action/workflow.

Alex can you change HardLight's forum title to a Hero Member? ;)

-craigz


krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 778
    • View Profile
    • Fat Pug Studio
Re: Fully Nestable SubFSM Communication
« Reply #4 on: August 16, 2018, 12:25:02 AM »
I never fiddled around too much with templates and sub fsms, so let's get this straight.

I have a template which is saved as an asset and by using Run FSM Func i can run that template to do stuff based on input, and then return the result as an output, which is cool since i don't need to have actually have an FSM on the game object, just the one to call Run FSM Func if i'm doing it without code.

But how do we define input and output? Some boilerplate code would be cool.

When i look at this, the possibilities it offers are huge!
« Last Edit: August 30, 2018, 10:40:30 AM by krmko »

TwisterK

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Re: Fully Nestable SubFSM Communication
« Reply #5 on: September 19, 2018, 10:50:06 PM »
This is awesome, it will make FSM template solution far more complete with this action. Thanks for sharing!