playMaker

Author Topic: Send Event from C# Script to Specific FSM by Name  (Read 617 times)

Rebooter

  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Send Event from C# Script to Specific FSM by Name
« on: October 03, 2019, 03:36:07 AM »
Is there a way to send an event by name to a specific FSM (also by name) from a C# script? I found PlayMakerFSM.BroadcastEvent() from a tutorial video, but it sends to all FSMs. I want to use a string array of FSMs.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 5945
    • View Profile
    • jinxtergames
Re: Send Event from C# Script to Specific FSM by Name
« Reply #1 on: October 03, 2019, 07:27:36 AM »
Hi.
maybe the PlayMaker API can help

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15443
  • Official Playmaker Support
    • View Profile
Re: Send Event from C# Script to Specific FSM by Name
« Reply #2 on: October 04, 2019, 05:13:16 AM »
Hi,

 Can you check out the PlayMaker utils from the Ecosystem, I created a series of c# apis to facilitate this.

and I just made one to fit your request:

PlayMakerUtils.SendEventToGameObjectFsmByName(null, <gameobject>, <fsm name>, <fsm event>,null);

Let me know how it goes :)

also, do you need this on a global scope and not target a specific GameObject? if yes, I can also make it, but it will be dangerous as only the first fsm found with that name will be called, but I guess I could make it that all fsm with that name gets called. let me know.

Bye,

 Jean

Rebooter

  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Re: Send Event from C# Script to Specific FSM by Name
« Reply #3 on: October 05, 2019, 10:22:19 AM »
I had to try several different approaches to solve the particular problem I was having and I finally got to storing the FSMs I needed to call the event on in a list, and I loop through the list and use 'fsmList.SendEvent("Init");'

I also use elsewhere:

var fsm = PlayMakerFSM.FindFsmOnGameObject(_fsmGO, fsmName.Value);
fsmInitManager.RegisterFsmInitState(fsm);

The reason I'm doing it like this is to make it easier to manage as it was taking 3-4 actions per FSM to do what I needed, so I wrote a few scripts to simplify it. This is to make sure certain FSMs have completed intializing some variables, etc., before the main FSM tries to use them.

I had noticed that when using breakpoints, sometimes I would get weird behavior. I think what was happening was that the breakpoint was doing something that affected the timing, in other words the stuff I had in the 'Start' states wasn't reliably being completed before the main FSM would send an event that I believe was causing the Start states not to complete.

But I see now that I shouldn't have assumed the 'Start' states would work in the way I was using them, so now I just have a blank state under the 'Start' event on certain FSMs, and I have an 'Init' event that the main FSM calls on each sub-FSM. Each sub-FSM is stored in a dictionary with a bool indicating if it has completed initializing. When all are true, the main FSM can continue to the next state.

Thank you for making the new action, that can be useful.

I don't currently need the 'global' option as I just needed to target specific FSMs (some on other gameobjects). Originally I was going to use strings for the FSMs and the Events, but now I'm getting references to the FSMs directly from within the action.
« Last Edit: October 05, 2019, 10:24:22 AM by Rebooter »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 5945
    • View Profile
    • jinxtergames
Re: Send Event from C# Script to Specific FSM by Name
« Reply #4 on: October 05, 2019, 12:16:51 PM »
Hi.
For something like you explained i usually have a separate fsm that has a 'Bool All True' action (every frame checked) and several bool variables. then from the other fsm's when they are ready i use a 'Set Fsm Bool' to set one of the variables on the fsm with the 'Bool All True' action.