Skip to content

Instantly share code, notes, and snippets.

@ApprenticeGC
Created June 2, 2013 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ApprenticeGC/5693533 to your computer and use it in GitHub Desktop.
Save ApprenticeGC/5693533 to your computer and use it in GitHub Desktop.
This code snippet illustrate the core of generating actions in PlayMaker programmatically. It is only dealing DebugLog action. Other actions that have event as the fields may need different approach. Besides, the code is not cleaned up yet.
// Just a method of the class
void TestAddProcess()
{
if (_theGameObject != null)
{
_theGameObject.AddComponent<PlayMakerFSM>();
PlayMakerFSM pmFsm = _theGameObject.GetComponent<PlayMakerFSM>();
if (pmFsm != null)
{
// Need to select fsm first
FsmEditor.SelectFsm(pmFsm.Fsm);
// Add states
FsmState fsmState1 = FsmEditor.Builder.AddState(new Vector2(200.0f, 200.0f));
FsmState fsmState2 = FsmEditor.Builder.AddState(new Vector2(200.0f, 250.0f));
FsmState fsmState3 = FsmEditor.Builder.AddState(new Vector2(200.0f, 300.0f));
FsmState fsmState4 = FsmEditor.Builder.AddState(new Vector2(200.0f, 350.0f));
fsmState1.Name = "Yesterady";
fsmState2.Name = "Today";
fsmState3.Name = "Tomorrow";
fsmState4.Name = "Forever";
// Add action
Type debugLogType = typeof(HutongGames.PlayMaker.Actions.DebugLog);
FsmStateAction fsmStateAction1 = FsmEditor.Builder.AddAction(fsmState1, debugLogType);
FieldInfo[] fieldInfos = ActionData.GetFields(debugLogType);
foreach (FieldInfo fieldInfo in fieldInfos)
{
if (fieldInfo.Name == "text")
{
if (ReflectionUtils.CanSetMemberValue(fieldInfo))
{
// Change value
ReflectionUtils.SetMemberValue(fieldInfo, fsmStateAction1, (FsmString)"Strange string");
}
}
}
List<FsmStateAction> actionList = new List<FsmStateAction>();
actionList.Add(fsmStateAction1);
// The key setp. Adjusted state action(s) have to be set back using svae.
fsmState1.ActionData.SaveActions(actionList.ToArray());
// Add transition
FsmEvent fsmEvent = new FsmEvent("FINISHED");
FsmTransition fsmTransition = FsmEditor.Builder.AddTransition(fsmState1, fsmState2.Name, fsmEvent);
// Set start state
pmFsm.Fsm.StartState = fsmState1.Name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment