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:
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?