playMaker

Author Topic: Adding custom actions to a FSM at runtime  (Read 829 times)

apalade

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Adding custom actions to a FSM at runtime
« on: August 01, 2016, 09:45:02 AM »
Hi,

I need some help with the following scenario:

I'm adding an FSM to a GameObject at runtime. This FSM is based on a template and I want to add custom actions to certain states. It all works like a charm except that Awake() is never called for the states I'm adding.

My code looks something like this:

Code: [Select]
var fsm = gameObject.AddComponent<PlayMakerFSM>();
fsm.SetFsmTemplate(fsmTemplate);

var state = fsm.FsmStates.ToList().Find(x => x.Name == "Using");
var actions = new List<FsmStateAction>(state.Actions);
actions.Add(customAction);
state.Actions = actions.ToArray();

I know why Awake() isn't being called but I'm wondering if there's a way to fix it. The call isn't happening because I'm initializing customAction (whose type inherits from FsmBaseAction) myself and I'm not calling Awake(). I imagine that Playmaker calls Awake() right after it creates every action object. I could of course just call Awake() myself right after I initialize customAction but this approach creates two problems:

  • It makes my code rely on emulated Playmaker behavior that might change in future versions.
  • I'm not actually initializing this action directly. I'm using a third party asset (Full Inspector) which allows me to configure the custom action directly from the Unity Editor. This is awesome but it means that I have no control over the instantiation code.

So all in all, I can work around this problem but I wonder if there's a better solution. Is there a "proper" way to add actions at runtime and have them go through their regular lifecycle?

apalade

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Re: Adding custom actions to a FSM at runtime
« Reply #1 on: August 10, 2016, 08:43:05 AM »
Anyone have any ideas or is this simply not supported?

The problem isn't just about Awake, it's about how to add a custom action at runtime and make sure it's properly initialized. Right now I'm emulating this by setting a bunch of properties (Fsm, State, Owner) but I realize how fragile this is.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2278
    • View Profile
    • jinxtergames
Re: Adding custom actions to a FSM at runtime
« Reply #2 on: August 11, 2016, 12:02:58 AM »
Hi,
I am not sure what you are trying to achieve, but maybe it would be better (if possible) to have your fsm already on your game object and use "enable fsm"
and also to have the custom action(s) already inside.

If you need multiple (custom) action possibilities you could use global int and a "int switch" before the custom actions to select the desired action.

apalade

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Re: Adding custom actions to a FSM at runtime
« Reply #3 on: August 11, 2016, 02:03:38 AM »
Hi,

That won't work for my scenario. I'll try to explain what I'm trying to achieve.

I have a lot of "parts" that make up a robot. Each part can have specific behavior but their states and transitions are identical. I've attached the FSM template that could be used by all of them.

So I have this template that I'd like to use for all the parts but I need different things to happen on "In Use" for each part. They could also have specific behavior for other states. I don't want to decouple the parts from the template because if something changes in the overall flow I would have to update all FSMs (and there are MANY parts).

So I suppose I'm trying to do a sort of inheritance: to use the common template but customize what happens in each state on a per-part basis. The solution I've attempted is to attach the FSM at runtime and then add the custom actions needed by each part to their particular FSM instance. It doesn't seem like this is actually supported by Playmaker but the fact that there are so many public methods threw me off.

I've spent way too much time on this problem though so I found another way to achieve my goal. It would still be nice to know if adding custom actions at runtime is supported or planned.