playMaker

Author Topic: Create and Add an Action to State via C# script [SOLVED]  (Read 2351 times)

joaobsneto

  • Playmaker Newbie
  • *
  • Posts: 6
Create and Add an Action to State via C# script [SOLVED]
« on: March 06, 2017, 09:40:29 PM »
 I'm generating a FSM Template via script using the following code:
Code: [Select]
fsmTemplate = (FsmTemplate)ScriptableObject.CreateInstance (typeof(FsmTemplate));
fsmTemplate.Category = "General";
fsmTemplate.fsm = new Fsm ();
fsmTemplate.fsm.DataVersion = 2;
fsmTemplate.fsm.UsedInTemplate = fsmTemplate;
fsmTemplate.fsm.Name = "newFsm";
AssetDatabase.CreateAsset (fsmTemplate, @"Assets\Resources\newFsm.asset");
AssetDatabase.SaveAssets ();
fsmTemplate = (FsmTemplate)AssetDatabase.LoadAssetAtPath(@"Assets\Resources\newFsm.asset", typeof(FsmTemplate));
FsmState IdleState = new FsmState (fsmTemplate.fsm);
IdleState.Name = "IdleState";
IdleState.Position = new Rect (30, 30, 60, 30);
FsmEvent fsmEvent1 = new FsmEvent ("Hail");
FsmTransition transition = new FsmTransition ();
transition.ToState = "Target";
transition.FsmEvent = fsmEvent1;
IdleState.Transitions = new FsmTransition[] { transition };
FsmState SecondState = new FsmState (fsmTemplate.fsm);
SecondState.Name = "Target";
SecondState.Position = new Rect (90, 90, 60, 30);
fsmTemplate.fsm.States = new FsmState[] { IdleState, SecondState };
fsmTemplate.fsm.StartState = "IdleState";
AssetDatabase.SaveAssets ();

It actually creates the FSM, but I get the following error:

Quote
System.NullReferenceException: Object reference not set to an instance of an object at HutongGames.PlayMaker.ActionData.GetParamDataType (System.Type type) [0x00302] in c:\Users\Alex\Documents\Unity\Playmaker\Projects\Playmaker.source.unity\Assets\PlayMaker\Classes\ActionData.cs:3185
When I try to add an Action it does not work. I was analysing the serialized data from a FSM template and the Action Serialization is done in "actionData". It's quite confusing and I can make it work right. Anyone have tried it before?

Thanks!
« Last Edit: March 07, 2017, 11:01:36 AM by joaobsneto »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Create and Add an Action to State via C# script
« Reply #1 on: March 07, 2017, 12:47:20 AM »
Hi,

 Unfortunatly, this api is not fully supported, so I am not sure what exactly is required to make this work, likely you are missing an initialization call on the fsm you created so that the action data has all its look up tables created.

 Bye,

 Jean

joaobsneto

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Create and Add an Action to State via C# script [SOLVED]
« Reply #2 on: March 07, 2017, 11:02:18 AM »
Actually, it was pretty easy:
Code: [Select]
State1.Actions = new FsmStateAction[] { action, action2 };
State1.SaveActions();
Thanks!!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Create and Add an Action to State via C# script [SOLVED]
« Reply #3 on: March 07, 2017, 12:01:43 PM »
Hi,

 Cool!!

Bye,

 Jean