playMaker

Author Topic: Extending FsmStateAction  (Read 4750 times)

imtrobin

  • Playmaker Newbie
  • *
  • Posts: 28
Extending FsmStateAction
« on: July 02, 2014, 01:53:16 PM »
Hi

Currently to extend an FSMStateAction, I have to write a separate script. When the state machines get big, and there's quite a of duplicate setup code.

Would it be possible to get them to be in one big file instead? Currently all the scripts extending it, I'm using it to forward to another big class that has all the methods in.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Extending FsmStateAction
« Reply #1 on: August 18, 2014, 04:21:44 AM »
Hi,

 I know some developers are putting all actions in one script ( one file), but I strongly advise against this as it makes impossible to playmaker to "edit" or "select" the custom action script, and it's a pain when you want to access them.

 Or maybe I don't understand what you are trying to do? could you share some pseudo code so that I get an idea of where you have your duplicated code? maybe a simple util class would do?

 Bye,

 Jean

imtrobin

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Extending FsmStateAction
« Reply #2 on: August 18, 2014, 02:38:27 PM »
I'm forwarding all the actions into one main script. I'm using Playmaker is to make a character state, which has quite a number of states, each a custom state action.

There is a lot of data setup shared between all of them so I put them in one single class, and I forward every custom state to it. This is done so I can code easily. Working with many custom state classes split, it's hard to code so putting them in a single class is necessary.

Code: [Select]
class CharacterState
{
// Data setup

public void OnIdleEnter ();
public void OnIdleExit ();
public void OnIdleUpdate ();

public void OnAttackEnter ();
public void OnAttackExit ();
public void OnAttackUpdate ();
};

class Idle : FSMStateAction
{
CharacterState cs;
override void Awake () { cs =owner.GetCompoment <CharacterState> ();}

//forward all class to Idle methods in cs.
}


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Extending FsmStateAction
« Reply #3 on: September 08, 2014, 08:06:13 AM »
Hi,

 I am not sure I see where is the hurdle here, simply have your custom actions communicating with that class and that's it. Typically, a manager in your scene should be responsible to host the instance of "CharacterState" and either expose a singleton property or a monobehavior for each action to have access to that. I do that all the time with manay third party assets.

 Bye,

 Jean

imtrobin

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Extending FsmStateAction
« Reply #4 on: September 08, 2014, 09:24:19 AM »
That's what I'm doing. For every state, I have to write a wrapper class extending FSMStateAction. It's a bit tedious, and the classes don't do much except forwarding calls, so if there is way to reduce these unnecessary classes.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Extending FsmStateAction
« Reply #5 on: September 09, 2014, 12:27:55 AM »
Hi,

 I don't think there is given how you have CharacterState defined, and actually, composition is something ok to do in scripting, it is tedious, but many times the only way.

If CharacterState was a monobehavior then you could use set and get property actions, but I would personnaly prefer spending the time creating direct code then rely on reflections too much. AND the great benefit of doing a proper playmaker action for each property means a cleaner and more readable Fsm and state. That's also to take in consideration for productivity in the long term. if a state has only set and get properties, it's very difficult to understand exactly what's going on.


Bye,

 Jean

imtrobin

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Extending FsmStateAction
« Reply #6 on: September 10, 2014, 04:52:06 AM »
I'm thinking of a different direction. Playmaker will have a list of states internally, stored in a dictionary (I''m guessing since I don't have source). So instead of subclassing an FSMAction, if you expose the OnEnter/Exit/Update as delegates that I can hook onto, then I could avoid these multiple composition bridge classes.

e.g Something like,

FSM.GetState ("Idle").OnEnter += MyEnterFunction;
FSM.GetState ("Attack").OnExit += MyExitFunction;
FSM.GetState ("CoolDown").OnUpdate += MyUpdateFunction;



jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Extending FsmStateAction
« Reply #7 on: September 12, 2014, 02:58:46 AM »
Hi,

 Yep, that would be a possible route, but I am not sure how that's going to save you from compositing or build reusable code, you'll have to host these assignments somewhere.

I'd like these assignments for events and variable changes as well, it would open a lot of possibilities for better bridges with third party frameworks.

 Bye,

 Jean

imtrobin

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Extending FsmStateAction
« Reply #8 on: September 12, 2014, 11:46:51 AM »
I can do it in the Awake function, all in one place instead of creating multiple files.

Yes. I take it would be implemented in future?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Extending FsmStateAction
« Reply #9 on: September 18, 2014, 09:28:37 AM »
Hi,

 Hopefully :)

 Bye,

 Jean